Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cellForRowAtIndexPath not called; sections returns 1 and rows returns 4

After parsing JSON data in a Data class, I set the UIViewController's NSArray *headlines property in a fillArrays method of the same Data class. In the viewDidAppear method of my UIViewController, I call reloadData on my UITableView. numberOfSectionsInTableView fires and returns 1, then numberOfRowsInSection fires and returns an array count of 4 (for 4 strings in the array). However, control never gets to cellForRowAtIndexPath and I'm having the hardest time understanding why, especially since I have valid sections and rows. The cells are all visible.

I've added the UITableViewDataSource and UITableViewDelegate protocols to the UIViewController interface and set the UITableView's delegate and dataSource to self in viewDidLoad (which also is verified by the row and section count methods being called).

I'm wondering if it has something to with me reinitializing the UIViewController in Data.m in order to set its properties.

In Data.m:

- (void)fillArrays:(NSArray *)jsonObjs {
    NSLog(@"fillArrays");               
    HeadlinesRootViewController *hrvc = [[HeadlinesRootViewController alloc] init];
    hrvc.headlines = [self getJsonValuesForKey:@"headline" inArrayOfObjects:jsonObjs];
    [hrvc viewDidAppear:NO];
}

In ViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"viewDidLoad");
    // Table view
    headlineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 180, self.view.bounds.size.width, 300) style:UITableViewStylePlain];
    [headlineTableView setDelegate:self];
    [headlineTableView setDataSource:self];
    // Temporary
    self.headlines = [[NSMutableArray alloc] initWithObjects:@"headline1", @"headline2", @"headline3", @"headline4", nil];



    [self.view addSubview:headlineTableView];
    self.headlineTableView = headlineTableView;
    [headlineTableView release];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"viewdidappear");
    NSLog(@"headlines: %@", self.headlines); // Returns an array of 4 headlines
    if( [self.headlines count] != 0 ){
        [self.headlineTableView reloadData];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"numberOfSectionsInTableView: 1");
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"numberOfRowsInSection: %d", [self.headlines count]);
    return [self.headlines count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    cell.text = [[NSString alloc] initWithFormat:@"%@", [self.headlines objectAtIndex:indexPath.row]];

    return cell;
}
like image 918
dvs Avatar asked Oct 22 '09 15:10

dvs


2 Answers

In fillArrays, you create another view controller - but you never do anything with it or its view, you would never see that view. You would never call viewDidAppear manually either, that happens automatically when a view controllers view is displayed (ONLY in the context of a navigation controller though).

Normally the flow is, you create a view controller and either add that view as a subview of a current view, or push it as a new window via a navigation controller. I'm pretty sure your whole issue is that they table is never added to a view anyone actually sees, so the table calls the other methods but never calls cellForRow because its layoutSubviews code is simply not being called.

like image 102
Kendall Helmstetter Gelner Avatar answered Sep 30 '22 05:09

Kendall Helmstetter Gelner


Have you added your tableView to the view of UIViewController?

It happened to me, and when I added this

[self.view addSubview:table];
[table release];

then cellForRowAtIndexPath started working.

like image 37
Josip B. Avatar answered Sep 30 '22 05:09

Josip B.