Objective C UITableView - table cells display incorrect content after changing cell height

I am trying to create an application in xcode that, like others, reads an rss feed and displays messages. I am new to objective-c, and sometimes it's a bit complicated.

I use NSMutableArray for retrieved stories (messages). Each story is represented by an NSMutableDictionary, which contains the title, subject, date and link of the message. All of them are displayed in the UITableView in the UIViewController. I set up my own cell, so I can display multiple shortcuts in them.

My problem is that if I use tableView: heightForRowAtIndexPath :, the first 5 cells (which are suitable for the screen) are displayed normally, but if you scroll down, the following cells will have the same content as the first 5 (that is, cells 0-4 displays OK , cell 5 has the contents of cell 0, cell 6 of cell 1, etc.)! If I remove tableView: heightForRowAtIndexPath: everything is just fine (except that I don't want to have a cell size)

This is what the code looks like:

// NavigationContentsViewController.h @interface NavigationContentsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { UITableView *myTableView; IBOutlet UITableView * newsTable; UIActivityIndicatorView * activityIndicator; CGSize cellSize; NSXMLParser * rssParser; NSMutableArray * stories; NSMutableDictionary * item; // it parses through the document, from top to bottom... NSString * currentElement; NSMutableString * currentTitle, * currentDate, * currentSummary, * currentLink; } @property(nonatomic,retain)NSMutableArray *itemsList; @property(nonatomic,retain)UITableView *myTableView; - (void)parseXMLFileAtURL: (NSString *)URL; 

.

 //NavigationContentsViewController.m - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Configure the cell. static NSString *MyIdentifier = @"MyIdentifier"; CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil){ cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; // Set up the cell int storyIndex = indexPath.row; //[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]]; //Story title //cell.textLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"]; //cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; cell.lTitle.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"]; cell.lSummary.text = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"]; cell.lDate.text = [[stories objectAtIndex: storyIndex] objectForKey: @"date"]; return cell; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *selectedCellItem = [NSString stringWithFormat:@"%d", indexPath.row]; TableViewController *fvController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:[NSBundle mainBundle]]; fvController.selectedCellItem = selectedCellItem; [self.navigationController pushViewController:fvController animated:YES]; [fvController release]; fvController = nil; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 80; } 

Any clues? [EDIT: changed int storyIndex = indexPath.row;]

+4
source share
2 answers

This is a common problem that people encounter when reusing a table cell.

this row is trying to reuse the cell. this means that if cell 0 moves off the screen, it will be reused as cell 5:

 CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

If the cell could not be reused, you create a new one:

 if (cell == nil){ cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 

and on the next line is your problem, you only set the cell if the cell cannot be reused. This happens 5 times (for cells that are visible when the table becomes visible).

But all the cells that your table wants to display after that will be reused cells that already have content.

  // Set up the cell /*...*/ 

but do not worry. it is very easy to fix. You must separate the creation of your cell from its configuration. Just change the code like this:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier"; CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil){ cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; } // whatever happened before. You have a valid cell at this point. // Set up the cell int storyIndex = indexPath.row; //[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]]; //Story title //cell.textLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"]; //cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; cell.lTitle.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"]; cell.lSummary.text = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"]; cell.lDate.text = [[stories objectAtIndex: storyIndex] objectForKey: @"date"]; return cell; } 

EDIT: maybe I should read the question next time. But I think I'm still 100% right.

If I remove tableView: heightForRowAtIndexPath: everything is just fine (except that I don't want to have a cell size)

I think this is a coincidence. How many cells do you have? I think around 7 or 8? Everything is normal, because all your cameras are visible at the same time. Therefore, there is no need to reuse the cell, and they all have content that they should have.

+11
source

Using [indexPath indexAtPosition ...] is most likely your source of errors - it does not get your correct index path.

However, if you create a CustomCell (hopefully in IB?), Then you should set the cell size in IB, and not do it in code.

+1
source

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


All Articles