Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iphone:UItableviewcell在滚动时重新加载_Iphone_Objective C_Ios_Uitableview - Fatal编程技术网

iphone:UItableviewcell在滚动时重新加载

iphone:UItableviewcell在滚动时重新加载,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,当我滚动我的tableview时,每个单元格中的所有数据都会重新加载。在tablecell的视图中它不会显示任何问题。 但这会导致从每个单元格检索的数量计算总数时出现问题。当我们滚动tableview时,总值会重新相加 这是我的代码: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITab

当我滚动我的tableview时,每个单元格中的所有数据都会重新加载。在tablecell的视图中它不会显示任何问题。 但这会导致从每个单元格检索的数量计算总数时出现问题。当我们滚动tableview时,总值会重新相加

这是我的代码:

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;

}
在计算值中:

-(void)calculateValues{

    itemPrice=itemPrice+pri;

    float subtot=quan * itemPrice;

    _subTotal.text=[NSString stringWithFormat:@"%.2f", subtot];


}

只需在.h文件中取一个变量,比如说mLastIndex,并将其初始值设置为-1

mLastIndex = -1;
然后在您的cellForRowAtIndexPath中更改为

if (indexPath.row > mLastIndex) {
            [self calculateValues]; 
            mLastIndex = indexPath.row;
        }

为什么不在cellforrowatindexpath方法中计算小计,即diplaying cell.textlable和detail lable的位置,然后再添加一个自定义标签并显示计算的结果subtotal@Inder库马尔·拉索尔谢谢你的回复。现在我只得到了最后一个电池的数量和价格。怎么能得到所有电池项目的总数呢?只需调试一下。。。将为新添加的行调用上述方法。。。如您所见,对于所有indexath.row>mLastIndex@Inder Kumar Rathore现在计算是正确的。当有更多的项目时,只有在完全滚动时,合计才会正确。即,只有当我们通过滚动访问它时,合计才会计算。如果您想显示所有对象的合计,为什么要将所有这些放在单元格中的行中,在视图中放置一个for循环并在那里计算所有值