我在每个单元格中都有一个带有3个标签的UITableView;活动名称,持续时间和满足。值取自用户输入,存储在activities数组中,然后显示在表中。我不确定该如何做的是总结“遇到”的价值。每个单元格中的值,并在tableView外的另一个标签中显示该总和。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellReuseIdentifier = @"CellReuseIdentifier";
TestCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil) {
cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
}
MyActivity *activity=(MyActivity *)[self.activities objectAtIndex:indexPath.row];
NSString *name = [NSString stringWithFormat:@"Activity Name: %@",activity.description];
NSString *value = [NSString stringWithFormat:@"Activity Duration: %d minutes",activity.duration];
NSString *met = [NSString stringWithFormat:@"Your MET: %d minutes",activity.duration* activity.intensity];
cell.activityLabel.text = name;
cell.durationLabel.text = value;
cell.metLabel.text = met;
return cell;
}
答案 0 :(得分:1)
单元格仅用于演示,不能从单元格中获取数据。我建议你编写一个函数来迭代你的数组来总结所有内容并更新表格视图之外的标签。
您可以编写自定义重新加载方法:
-(void)computeAndReloadData
{
[self computeSum];
dispatch_async(dispatch_get_main_queue(), ^{
[tableView reloadData];
});
}
答案 1 :(得分:0)
您可以快速枚举数组
CGFloat metTotal;
for (MyActivity *eachAct in self.activities) {
CGFloat met = eachAct.duration * eachAct.intensity;
metTotal = metTotal + met;
}
label.text = [NSString stringWithFormat:@"%f",metTotal];
答案 2 :(得分:0)
从数组中计算总和,如:
-(int)sum
{
int sum = 0;
for(int i = 0; i < self.activities.count; i++)
{
MyActivity *activity=(MyActivity *)[self.activities objectAtIndex:i];
sum += [activity.duration intValue];
}
return sum;
}
答案 3 :(得分:-1)
试试这个
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
double totalMet = 0.0f;
for (MyActivity* aActitivity in activities) {
totalMet = totalMet + (activity.duration* activity.intensity);
}
outsideLabel.text = [NSString stringWithFormat:@"%f",totalMet];
return [activities count];
}