Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView willDisplayCell - something strange is going on

I have my datasource (productsArry) which is populated via SQLite and I'm fetching 25 items at a time. I'm calling the 'willDisplayCell' UITableView delegate and when the indexPath.row is equal to the productsArry count it will fetch new data and reload the tables view. This works great with no problems but lets say if the datasource has 100 objects, which are being displayed in the tableview and then the productsArry has been filtered with less objects (say 50) and do a reloadData, which is fine again, but the problem is within the willDisplayCell delegate. For some reason this method is being called without the user scrolling. So it thinks cell 25 has been scrolled too so now it will fetch the next set of rows from the database. It will keep on doing this until it reaches the 100 objects from the last set of data or theres nothing else to fetch from the filter.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell    forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (([self.productsArry count] != 0) && (indexPath.row == [self.productsArry count])) 
    {
        self.currentOffset += TableviewRowCount;
        [self performSelector:@selector(reloadTableView:) withObject:nil afterDelay:0.2];
    }
}

- (void)reloadTableView:(id)sender
{
    NSLog(@"reloadtable");
    [self fetchData];
    [self.productTableView reloadData];
}

Please let me know if you need to know anything else that might help to clear things up.

like image 581
Peter Avatar asked Dec 13 '22 08:12

Peter


1 Answers

I found the main issue here is that the table does not know the height of the cell so it assume the default of OS and the cell will be disabled , after the table calculate the layout it get the correct height.

So to fix this issue just add the

self.tableView.estimatedRowHeight = 100 // or your estimate 
like image 184
Omar Freewan Avatar answered Feb 24 '23 05:02

Omar Freewan