我有一个 UITableView,每行都有一个带有浮点值的标签。最后一行中的最后一个标签应显示总金额。
它显示的总金额很好,但如果 tableView 滚动到屏幕外并返回,金额会增加。
if(tableView == self.allTab)
{
if(indexPath.section == 0)
{
self.firstLabel.text = @"Category";
self.firstLabel.textColor = [UIColor redColor];
self.secondLabel.text = @"Date";
self.secondLabel.textColor = [UIColor redColor];
self.thirdLabel.text = @"Amount";
self.thirdLabel.textColor = [UIColor redColor];
}
else if(indexPath.section == 1)
{
NSManagedObject *records = nil;
records = [self.listOfExpenses objectAtIndex:indexPath.row];
self.firstLabel.text = [records valueForKey:@"category"];
NSString *dateString = [NSString stringWithFormat:@"%@",[records valueForKey:@"date"]];
NSString *dateWithInitialFormat = dateString;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
self.secondLabel.text = dateWithNewFormat;
NSString *amountString = [NSString stringWithFormat:@"%@",[records valueForKey:@"amount"]];
self.thirdLabel.text = amountString;
totalAmount = totalAmount + [amountString doubleValue];
}
else if(indexPath.section == 2)
{
self.firstLabel.text = @"Total";
self.firstLabel.textColor = [UIColor redColor];
self.thirdLabel.text = [NSString stringWithFormat:@"%.2f",totalAmount];
self.thirdLabel.textColor = [UIColor redColor];
self.thirdLabel.adjustsFontSizeToFitWidth = YES;
}
}