简体   繁体   中英

tableView cellForRowAtIndexPath called multiple times initially

I am trying to understand how the rendering of each cell works. Here's my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"QuestionCell";
    static NSString *cellNib = @"QuestionsTableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellNib owner:self options:nil];
        cell = [nib objectAtIndex:0];        
        NSLog(@"%@", @"--> load from nib");
    }
    return cell;
}

The NSLog in this code is called 8 times initially (because I have 8 rows visible initially). Then when I start scrolling, it's called once and then no more after. I thought it should've been called once and that's it since it should be re-using it after the first call? And why is it called once more after the initial 8? (scratching head...)

It's possible that there could be 9 cells on screen when you are scrolling the table view. This wasn't needed when you started but as you scroll, the 9th cell is required as the top one leaves, a new cell at bottom enters. A cell can't be reused in halves. :)

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