views:

68

answers:

2

Hello everyone. I'm a beginner, so please excuse me if I'm missing something obvious :-)

The title says it all. The table I'm having problem with is pushed from another table controller, and there is only one section in this table, and I'm using custom cells. Here is the code for numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger numberOfRows;
if ([imagesArray count] == 0) {
    numberOfRows = 0;
}
else {
    if ([imagesArray count] % 4 == 0) {
        numberOfRows = [imagesArray count] / 4;
    }
    else {
        numberOfRows = ([imagesArray count] / 4) + 1;
    }
}
NSLog(@"numberOfRowsInSection: %d", numberOfRows);
return numberOfRows;
}

The log here always prints the expected number of rows. So far so good. Now let's take a look at cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellForRowAtIndexPath: %@", [indexPath description]);

    ImageTableCell *cell = (ImageTableCell *)[tableView dequeueReusableCellWithIdentifier:@"4ImagesCell"];
    if (cell == nil) {
            cell = [[[ImageTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"4ImagesCell"] autorelease];
    }
    return cell;
}

The console here "always" prints the following:

cellForRowAtIndexPath: <NSIndexPath 0x5a4f2c0> 2 indexes [0, 0]
cellForRowAtIndexPath: <NSIndexPath 0x5a4f2c0> 2 indexes [0, 1]
cellForRowAtIndexPath: <NSIndexPath 0x5a4f2c0> 2 indexes [0, 2]
cellForRowAtIndexPath: <NSIndexPath 0x5a4f2c0> 2 indexes [0, 3]
cellForRowAtIndexPath: <NSIndexPath 0x5a4f2c0> 2 indexes [0, 4]

No matter what the value returned by numberOfRowsInSection is, whether it is a Zero or a 100, the table always asks for 5 rows! I even tried returning a static number in numberOfRowsInSection instead of the current conditions, but it makes no difference for the actual number of cells!

What drives me crazy is that the table does reach the numberOfRowsInSection method before going to cellForRowAtIndexPath!

The only thing I can think of that maybe causing this is that this table controller is pushed from another UITableViewController, but the previous table's numberOfRowsInSection method is never checked while I'm in this table, I even tried to modify the number of rows in the parent table without any luck.

If there is any kind of info missing please let me know. The project is quit complicated and I can't think of any other relevant info for this problem.

Any kind of help is very much appreciated.

A: 

Can you try logging it as this instead?

NSLog(@"cellForRowAtIndexPath: (section:%@, row:%@)", [indexPath section], [indexPath row]);

I think what we are looking at as the output of the description method may not be what you think it means.

Ankur
A: 

That's because the tableview only loads the visible cells. Have you tried scrolling your tableview? I'll bet if you do that, you'll see more rows get loaded...

Dave DeLong
Oh yeah, I figured this out the next day. Next time I really should stop writing/debugging code while I'm sleepy. Thanks Dave and Ankur for your kind reply.
Mota