UITableView provides an empty table, does not load data

Everything works fine when the view containing my table is the main (first) view.

However, when this is not the first view, and I switch to this view, my table does not load data, and I get an empty table .

Using NSLog, I can say that the program does not call numberOfRowsInSection and cellForRowAtIndexPath .

I have <UITableViewDataSource, UITableViewDelegate>, IBOutlet UITableview *tableViewall declared. They are also connected in InterfaceBuilder.

I tried using viewWillAppearand [tableView reloadData], but that didn't help.

I am new to iPhone development and your help is appreciated!

UPDATE:

I tried [tableView reloadData] but nothing happened.
I don't release tableView anywhere but dealloc.

Here is the code:
AppDelegate

//
//  tpbAppDelegate.m
//  tpb


#import "listController.h"
#import "tpbAppDelegate.h"
#import "tpbViewController.h"



@implementation tpbAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize navController;
@synthesize toolbar;
@synthesize btnMyLoc;

@synthesize places; //array that holds data from the XML file


- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    //add places into an array that can be used by other views (Table)
    rssList = [[NSMutableArray alloc] initWithCapacity:1];  

    NSString *paths = [[NSBundle mainBundle] resourcePath];
    NSString *xmlFile = [paths stringByAppendingPathComponent:@"tourplay.xml"];

    NSURL *xmlURL = [NSURL fileURLWithPath:xmlFile isDirectory:NO];
    NSLog(@"DATA URL:  %@", xmlURL);

    NSXMLParser *firstParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [firstParser setDelegate:self];
    [firstParser parse];


    // Resize window for toolbar:    

    CGRect frame = viewController.view.frame;
    frame.size.height -= toolbar.frame.size.height;
    viewController.view.frame = frame;

    //[window addSubview:viewController.view];
    navController.viewControllers = [NSArray arrayWithObject:viewController];
    navController.view.frame  = frame;
    [window addSubview:navController.view];
    [window makeKeyAndVisible];
}

- (IBAction)showList:(id)sender{
    //Switch to table view on segment control change
    UISegmentedControl *segmentControl = (UISegmentedControl *)sender;
    NSString *curSelection = [NSString stringWithFormat:@"%d", [segmentControl selectedSegmentIndex]];
    //[segmentControl titleForSegmentAtIndex: [segmentControl selectedSegmentIndex]]];
    NSLog(@"pressed button %@", curSelection);
    if ([curSelection isEqualToString:@"1"]){
        NSLog(@"TABLE SELECTED");
        listController *listTable = [[listController alloc] init];
        [navController pushViewController:listTable animated:YES]; //SWITCH TO TABLE VIEW (listController)
    } else {
        tpbViewController *tpb = [[tpbViewController alloc] init];
        NSLog(@"MAP SELECTED");
        [navController pushViewController:tpb animated:YES];
    }


}
#pragma mark Praser Methods
//Parse XML into an array
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    //NSLog(@"Started parsing");
    if ([elementName compare:@"tour_point"] == NSOrderedSame) {

        [self.places addObject:[[NSDictionary alloc] initWithObjectsAndKeys:
                                //[attributeDict objectForKey:@"tour_point_id"],@"tour_point_id",
                                [attributeDict objectForKey:@"name"],@"name",
                                [attributeDict objectForKey:@"tour_html"],@"tour_html",
                                [attributeDict objectForKey:@"audio_src"],@"audio_src",
                                nil]];

    } else if ([elementName compare:@"title"] == NSOrderedSame) {
        titlename = (NSString *)[attributeDict objectForKey:@"titlename"];      
        NSLog(@"Done parsing %@ points", titlename);
    }

}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"Parser end");

    [parser release];

}

- (void)dealloc {
    [viewController release];
[rssList release];
    [places release];
    [toolbar release];
    [window release];
    [super dealloc];
}


@end

listController.h - table class

#import <UIKit/UIKit.h>


@interface listController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
    UITableView *tableView;
    NSMutableArray *places;
    NSString *titlename;
}
@property (nonatomic, retain) IBOutlet UITableView *tableView;

@end

listController.m - table implementation

//
//  listController.m
//  tpb
//

//

#import "listController.h"
#import "tpbAppDelegate.h"


@implementation listController

@synthesize tableView;



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
//- (void)viewWillAppear {


    tpbAppDelegate *delegate = (tpbAppDelegate *)[[UIApplication sharedApplication] delegate];
    places = delegate.places;
    NSLog(@"Loaded table view");
      [super viewDidLoad];
//  [tableView reloadSectionIndexTitles];


}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tv {
    NSLog(@"Number of secions");
    return 1;
}


- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section {
    NSLog(@"GETTING COUNT");


    return [places count];

}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Assigning Cells");
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }


    cell.textLabel.text = [[places objectAtIndex:indexPath.row] objectForKey:@"name"];
    //NSLog(@"count %@", [[places objectAtIndex:indexPath.row] objectForKey:@"name"]);
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
    NSLog(@"ROW clicked");
    [tv deselectRowAtIndexPath:indexPath animated:YES];

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];


}

- (void)viewDidUnload {
    NSLog(@"Unloaded tableview");

}


- (void)dealloc {
    [tableView release];
    [places release];
    [super dealloc];
}


@end

- Until now, I know that loadDidLoad is loading, but meathods tables, such as cellRowAtIndexPath, are not called.

+3
source share
3 answers
- (IBAction)showList:(id)sender{
    //Switch to table view on segment control change
    UISegmentedControl *segmentControl = (UISegmentedControl *)sender;
    NSString *curSelection = [NSString stringWithFormat:@"%d", [segmentControl selectedSegmentIndex]];
    //[segmentControl titleForSegmentAtIndex: [segmentControl selectedSegmentIndex]]];
    NSLog(@"pressed button %@", curSelection);
    if ([curSelection isEqualToString:@"1"]){
        NSLog(@"TABLE SELECTED");
        listController *listTable = [[listController alloc] init];

   //Try this
   listTable.places = self.places; // set the array contents here and check

        [navController pushViewController:listTable animated:YES]; //SWITCH TO TABLE VIEW (listController)
    } else {
        tpbViewController *tpb = [[tpbViewController alloc] init];
        NSLog(@"MAP SELECTED");
        [navController pushViewController:tpb animated:YES];
    }
}

Remember to write accessors in ListController for an array of places. And let me know what will happen.

0
source

, . . , , .

, ,

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

. , 0 . '0' iphone-sdk-3.0 .

,

Madhup

+3

, reloadData; / , , / , .

0

Source: https://habr.com/ru/post/1740259/


All Articles