0

Im getting json data from the backend , and displaying them in the tableview. each object is displayed in a cell, some objects have same names, other have different.

the problem is when I am scrolling the tableview, the object names are changing. My code in cellForRow atIndex path:

static NSString *cellIdentifier = @"cell2";
ProccessingOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
Order *order = self.orderArray[indexPath.row];
NSString *role = [NSUserDefaults.standardUserDefaults objectForKey:@"userRole"];
if([role isEqualToString:@"brand.operator"]){
    cell.branchName.hidden = NO;
    NSString *roleDisplay = [NSString stringWithFormat:@"%@%@", @"Branch name: " ,order.branch.name ] ;
    cell.branchName.text = roleDisplay;
} else if([role isEqualToString:@"corporate.operator"]){
    cell.branchName.hidden = NO;
    NSString *roleDisplay = [NSString stringWithFormat:@"%@%@ \n%@%@", @"Branch name: " , order.branch.name,@"Brand name: ",order.branch.brand.name ] ;
    cell.branchName.text = roleDisplay;
} else {
    cell.branchName.hidden = YES;
}

the field "cell.branchName.text" is changing on scrolling,

Any idea whats happening?

1 Answers1

0

Cell gets reused when you scroll in UITableView and UICollectionView. Hence you see the old text for fraction of second and gets updated to proper value.

Use prepareForReuse of UITableViewCell method to clear the textFields before reuse to avoid such issues

In your ProccessingOrderTableViewCell write

-(void) prepareForReuse {
    [super prepareForReuse];
    //clear your textfields here
    self.branchName.text = @"";
   //clear/reset other UI components as well
}
Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • As you see in his question, he handle every case to update `self.branchName` in `cellForRowAtIndexPath` so `prepareForReuse` in this case is useless. – trungduc Nov 10 '17 at 08:59
  • so whats the solution? @ trungduc –  Nov 10 '17 at 12:59