简体   繁体   中英

UITableViewController numberOfRowsInSection doesn't affect the actual number of rows

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.

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM