我正在购买购物应用,我在购物车中添加商品。现在我必须显示我需要在表中添加每个项目的价格的子总金额。
如何从每个单元格中选择值并添加它们?
以下是tableview的代码:
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [CartList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *cellData = [[[CartList objectAtIndex:indexPath.row] valueForKey:@"AddtoCartProductImageModel"] firstObject];
NSString *image=[cellData valueForKey:@"productimg"];
NSString *urlString = [NSString stringWithFormat:@"http://dealnxt.com/areas/user/productimage/%@", image];
NSURL *Url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:Url];
NSString *CellData = [[CartList objectAtIndex:indexPath.row] valueForKey:@"shortdescription"];
static NSString *simpleTableIdentifier = @"Cell";
CartTableViewCell *cell = (CartTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath: indexPath];
cell.proimg.image=[UIImage imageWithData:data];
cell.prlbl.lineBreakMode = NSLineBreakByWordWrapping;
cell.prlbl.numberOfLines = 0;
cell.prlbl.text=CellData;
cell.prprice.text=[NSString stringWithFormat: @"₹%@",[[CartList objectAtIndex:indexPath.row] valueForKey:@"price"]];
cell.quantity.text=[NSString stringWithFormat:@"%@",[[CartList objectAtIndex:indexPath.row] valueForKey:@"quantity"]];
NSString *string =[NSString stringWithFormat: @"%@",[[CartList objectAtIndex:indexPath.row] valueForKey:@"price"]];
int value = [string intValue];
for (int i=0; i<=[CartList count]; i++) {
int Total=0;
Total=+value;
NSLog(@"total:%d",Total);
}
return cell;
}
priki是我需要添加的地方。
答案 0 :(得分:1)
// write this code somewhere where you fill the CartList variable value.
float tot;
for (int i = 0; i < [CartList count]; i++)
{
tot = tot + [[[CartList objectAtIndex:i] valueForKey:@"price"] floatValue];
// Do stuff...
}
NSLog("Total Cost : ₹ %.2f",tot);
答案 1 :(得分:1)
我们不知道CartList数组中的对象类是什么,但代码看起来像这样:
double total = 0;
for (anItem in CartList) {
total += (double) anItem["price"];
}