滚动tableview时,自定义tableview单元格中的标签值正在变化
static NSString *cellIdentifier = @"Cell";
cell = (CheckOutCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// If there is no cell to reuse, create a new one
if(cell == nil)
{
cell = [[CheckOutCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.delegate = self;
CheckOutGroup * checkOut=_cartArray[indexPath.row];
cell.name.text=checkOut.name;
cell.size.text=checkOut.size;
cell.priceValue=checkOut.price ;
[array addObject:checkOut.dictionary];
[itemIdArray addObject:checkOut.itemId];
cell.price.text=[NSString stringWithFormat:@"Price:%@%@",self.currValue,checkOut.price];
cell.quantity.text=[NSString stringWithFormat:@"Qty:%d",1];
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://sqwip.ignivainfotech.net/%@",checkOut.image]]];
cell.checkOutImage.image=[UIImage imageWithData:imgData];
NSUserDefaults *storeDefaults = [NSUserDefaults standardUserDefaults];
[storeDefaults setObject:self.currValue forKey:@"cur"];
cell.increaseBtn.tag=indexPath.row;
val=[checkOut.price intValue];
if (check==0) {
check=check+[checkOut.price intValue];
}
else
check=check+[checkOut.price intValue];
self.totalPrice.text=[NSString stringWithFormat:@"TotalPrice:%@%d",self.currValue,check];
return cell;
}
答案 0 :(得分:0)
当需要显示一个单元格(IE滚动回到屏幕上)时,tableview控制器将查看数据源并激活其中的一些方法,以便它可以显示正确的信息。简而言之,每次需要显示一个单元格时,它都会调用此方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
如果正在更改,则数据源正在发生变化!我建议在cellForRow中放置一个断点,并确保来自你的数据:
CheckOutGroup * checkOut=_cartArray[indexPath.row];
或滚动前后右侧单元格的正确值。如果它不同而且它不应该是,可能你在下一次传递中从URL中获取错误的数据或者你的索引不合适。
答案 1 :(得分:0)
It's because of re-useability. Since UITableView reuse its cell on scrolling. To resolve this just make your cell nil.
Try this on CellForRowAtIndexPath:-
static NSString *cellIdentifier = @"Cell";
cell = (CheckOutCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell)
cell = nil
// If there is no cell to reuse, create a new one
if(cell == nil)
{
cell = [[CheckOutCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}