Abirami Iruthayaraj
Abirami Iruthayaraj

Reputation: 1

Duplicate rows on uitableview when scrolling with custom cell

Here is my code :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 NSString *cellIdentifier=@"menucell";

MenuCell *cell = (MenuCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

if (cell==nil)
    cell= [[MenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;

MenuModel *resturntent = (self.menuList)[indexPath.row];
cell.foodNameLbl.text = resturntent.MenuName;
cell.priceLbl.text = [NSString stringWithFormat:@"%@ %@",@"$" , resturntent.MenuRate];
cell.foodImage.image = [UIImage imageNamed:@"full_breakfast.jpg"];
return cell;
}

While scrolling uitextfields values gets duplicates .

I need help to fix this issues

Upvotes: 0

Views: 430

Answers (1)

7vikram7
7vikram7

Reputation: 2824

When you use dequeueReusableCellWithIdentifier, your cells will be reused. This means when the user scrolls the tableview, a cell which moves out of the screen will be reused to display the contents of a cell which is about to move onto the screen.

Even though this helps in saving memory, the problem it causes is the that the cell needs to be prepared for display before it is loaded with the content of the new cell.

In your case, it seems you need to maintain the values the user has entered in a textfield of a cell.

So to fix your problem, if there are not that many cells in the tableview, simply stop reusing the cell. Else whenever the user enters a value in the textfield of a cell, save it in an array (array of 'MenuModel' in your case). And fetch the value from from the model and set it to your textfield in the cellForRowAtIndexPath method like your have set values for other labels.

Upvotes: 1

Related Questions