After scrolling, the images of the user cells of the UITableView disappear.

I create an open view of the grid.

I created a custom cell that looks like this:

enter image description here

I treat it like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"Cell"; TableGalleryCustomCell *cell = (TableGalleryCustomCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if( cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableGalleryCustomCell" owner:nil options:nil]; for (id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[UITableViewCell class]]) { cell = (TableGalleryCustomCell*)currentObject; break; } } } if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) { cell.firstAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address]; cell.firstPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit]; if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) { [cell.firstImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]]; cell.firstImage.tag = countingIndex; } } countingIndex++; if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) { cell.secondAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address]; cell.secondPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit]; if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) { [cell.secondImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]]; cell.secondImage.tag = countingIndex; } } countingIndex++; if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) { cell.thirdAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address]; cell.thirdPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit]; if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) { [cell.thirdImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]]; cell.thirdImage.tag = countingIndex; } } countingIndex++; if (countingIndex < [[[self.appDelegate rssParser] rssItems] count]) { cell.fourthAddress.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Address]; cell.fourthPrice.text = [[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] Deposit]; if ([[[[[[self appDelegate] rssParser]rssItems]objectAtIndex:countingIndex] imageURLs] count] != 0 ) { [cell.fourthImage setImageWithURL:[NSURL URLWithString:[[[[[self.appDelegate rssParser] rssItems] objectAtIndex:countingIndex] imageURLs] objectAtIndex:0.0]]]; cell.fourthImage.tag = countingIndex; } } countingIndex++; return cell; } 

It's a little dirty, but it works ..... until I scroll. After loading the image, then the screen scrolls, the image disappears.

I suppose this could be due to the fact that the image of the Views from the cells is lost?

I tried the exact same implementation, but with only one imageView in each cell, and everything works fine.

Any point in the right direction would be greatly appreciated.

I appreciate any help and thank you for your time.

+4
source share
2 answers

I believe the problem is that your cells are being tableView:cellForRowAtIndexPath: , and you are filling cells in tableView:cellForRowAtIndexPath: using tableView:cellForRowAtIndexPath: information other than indexPath.row.

Perhaps try something on the following lines:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSArray * rssItems; id rssItem; static NSString * MyIdentifier = @"Cell"; TableGalleryCustomCell *cell = (TableGalleryCustomCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if( cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableGalleryCustomCell" owner:nil options:nil]; for (id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[UITableViewCell class]]) { cell = (TableGalleryCustomCell*)currentObject; break; } } } rssItems = [[self.appDelegate rssParser] rssItems]; if (indexPath.row < [rssItems count]) { rssItem = [rssItems objectAtIndex:indexPath.row]; cell.firstAddress.text = [rssItem Address]; cell.firstPrice.text = [rssItem Deposit]; if ([[rssItem imageURLs] count] != 0 ) { [cell.firstImage setImageWithURL:[NSURL URLWithString:[[rssItem imageURLs] objectAtIndex:0.0]]]; cell.firstImage.tag = indexPath.row; } } return cell; } 
+2
source

Experience exchange:

There were similar problems. I have many tables in the application, and in one of them the data will randomly disappear, and in the end the whole table will go away.

The problem for me was that I deactivated the cells, giving them the same "unique" identifier. Therefore, several tables shared the same identifier, and I believe that they conflict and ruined the cells that I was looking at.

Giving each table its own unique identifier, I solved the problem. (D!)

+3
source

Source: https://habr.com/ru/post/1392594/


All Articles