Reputation: 120
I have a custom cell. It has two text fields and button, all of them I created in my storyboard. But when I scroll my next cell appears with new text but with old button(with button from the first cell).
According to many tutorials this repeatable cell must be fixed using this rows:
LessonCell *cell = (LessonCell *)[self.lessonTV dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[LessonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
But in my case it's not enough, it doesn't work and my cells repeat when I scroll, could you help me?
One more point: repeats only the first cell((
Upvotes: 0
Views: 46
Reputation: 7552
If you have a custom class to represent the cell, you can override the -[UITableViewCell prepareForReuse]
method to reset the cell when it's dequeued.
A side note: You shouldn't use
[tableView dequeueReusableCellWithIdentifier:]
you should be using
[tableView dequeueReusableCellWithIdentifier:forIndexPath:]
The latter is not deprecated, and it has some runtime checks that will help you out.
Upvotes: 2
Reputation: 3956
When you first create the cell, it will be initialised with some values. So when you scroll down it will dequeue the same old cell, and since you aren't modifying it, same old contents will be shown. You can have a updateCell method which updates ui elements and content each time you load that cell.
LessonCell *cell = (LessonCell *)[self.lessonTV dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[LessonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[self updateCell];
-(void)updateCell{
//do all content and ui updations here
}
Upvotes: 0