Reputation: 2467
When I scroll my tableview,the whole data in each cell reloads.It doesn't show any problem in tablecell's view. But it causes issues in calculating the total from the quantities retrieved from each cell.The total value re-adds when we scroll the tableview.
this is my code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; //subtitle view supprot
}
//Get the object from the array.
order *orderObj = [appDelegate.orderArray objectAtIndex:indexPath.row];
//Set the ordername.
cell.textLabel.text = orderObj.itemName;
cell.detailTextLabel.text=[NSString stringWithFormat:@"Quantity : %@ Price : %@ Spiciness : %@", orderObj.quantity,orderObj.price,orderObj.spiciness];
quan=[orderObj.quantity intValue];
pri=[orderObj.price floatValue];
[self calculateValues];
return cell;
}
In calculate values :
-(void)calculateValues{
itemPrice=itemPrice+pri;
float subtot=quan * itemPrice;
_subTotal.text=[NSString stringWithFormat:@"%.2f", subtot];
}
Upvotes: 0
Views: 122
Reputation: 39978
Just take a variable in .h file say mLastIndex and set it's initial value to -1
mLastIndex = -1;
and then change in your cellForRowAtIndexPath as
if (indexPath.row > mLastIndex) {
[self calculateValues];
mLastIndex = indexPath.row;
}
Upvotes: 1