gpt4 book ai didi

ios - 从 tableview 的每个单元格中执行值的添加

转载 作者:行者123 更新时间:2023-11-28 21:16:12 25 4
gpt4 key购买 nike

我正在开发一个购物应用程序,我可以在购物车中添加商品。现在我必须显示小计金额,我需要在表中执行每个项目的价格相加。

如何从每个单元格中选取值并相加?

下面是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;
}

prprice 是我需要添加的。

最佳答案

    //  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);

关于ios - 从 tableview 的每个单元格中执行值的添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41275823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com