Weird UITableViewBehavior

Why is it if I have a UITableView and I display cells that it fires the willDisplayCell for every cell that is in the table? One cell takes up about 40% of the display. So at most 4-5 cells should be prepared and fire the cellForRowAtIndexPath method along with the willDisplayCell method. It shouldn't fire all 15! It's killing the performance of my tableview.



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // vars
    SimpleTVCell *tempCell = (SimpleTVCell *)[tableView dequeueReusableCellWithIdentifier:simpleTVCellIdentifier];
    NSLog(@"temp cell %@", tempCell);
    
    // return
    return tempCell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    // vars
    SimpleTVCell *tempCell = (SimpleTVCell *)cell;
    
    NSLog(@"will display cell %d", indexPath.row);
}

Replies

I have tested with a table with 50 rows.

There is room on screen to display about 12.


The func is

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
       
        print(#function, indexPath.row)
    }


When loading, I just get willDisplay called for 12 rows. Not the whole table.

And then, when scrolling, additional rows are handled in will display.


So, behaves as expected, not as your case.A few questions:

- which version of XCode ?

- Have you defined the estimatedRowHeight ?

https://developer.apple.com/documentation/uikit/uitableview/1614925-estimatedrowheight


This discussion on how those API work may interest you:

h ttps://jobs.zalando.com/tech/blog/proper-use-of-cellforrowatindexpath-and-willdisplaycell/?gh_src=4n3gxh1