UITableView loads the same cells for some reason

I have a TableView that loads a custom cell and loads data from a JSON string on the server. The JSON string is parsed into an array of 14 identifiers (id_array).

If cell==nil , then I use [id_array objectAtIndex:indexPath.row] to get the identifier and get more information about this line from the server and configure the labels and images of the cells.

When the application starts, UITableView loads the visible lines [0,1,2,3,4] (cell height - 70 pixels). When scrolling through the TableView, row [5] loads and retrieves data from the server, but the problem is that beyond this point - TableView repeats these 6 rows instead of requesting new data from the server for new rows ...

But it requests new data for the line [5], which is not displayed (and not loaded) when the application is first launched.

Does anyone know why this is happening? Thanks!

EDIT: Here is my cellForRowAtIndexPath method

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; for (id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[UITableViewCell class]]) { cell = (CustomCell *)currentObject; NSString *appsURL = [NSString stringWithFormat:@"http://myAPI.com?id=%@",[app_ids objectAtIndex:indexPath.row]]; NSLog(@"row -> %d | id -> %@",indexPath.row,[app_ids objectAtIndex:indexPath.row]); NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:appsURL]]; [cell updateAppInfoForCellWithRequest:request]; break; } } } // Configure the cell... return cell; } 
0
source share
1 answer

If you only set data if cell==nil , then this is your problem. UITable creates a cache for table cells, creating a new one if the cell is zero. Therefore, you must set your data each time, i.e. Outside the cell==nil block.

The following example shows this process. First, take a cell from the pool, if there is no free cell, create a new one. Set the cell values ​​for the corresponding row.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } id someData = [id_array objectAtIndex:indexPath.row] cell.textLabel.text = [someData someString]; return cell; } 
+3
source

All Articles