Ios 每当我单击该行时,增加tableview单元格中的clickcount标签值

Ios 每当我单击该行时,增加tableview单元格中的clickcount标签值,ios,swift,Ios,Swift,在我的CustomCell中有一个clickCount标签,我想在行click上增加它的值。 假设这个标签的当前值是5,那么我想在单击行时增加它,同时在表中更新它。 我正在为此使用reloadRowsAtIndexPaths,但它不会更新标签文本,在此之后调用cellForRowAtIndexpath,并用以前的值重置标签 -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nonnull NSIndexPat

在我的
CustomCell
中有一个
clickCount
标签,我想在
行click
上增加它的值。 假设这个标签的当前值是5,那么我想在单击行时增加它,同时在表中更新它。 我正在为此使用
reloadRowsAtIndexPaths
,但它不会更新标签文本,在此之后调用
cellForRowAtIndexpath
,并用以前的值重置标签

   -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
    {
      NSString *Str= [[ArrProductInCart objectAtIndex:indexPath.row] valueForKey:@"Product_Count"];
int Quantity=[Str intValue];
        Quantity=Quantity+1;

        StrQuantity=[NSString stringWithFormat:@"%d",Quantity];

        CGPoint correctedPoint = [sender convertPoint:sender.bounds.origin toView:tbl];
        NSIndexPath *indexPath = [tbl indexPathForRowAtPoint:correctedPoint];

        [self updateCell:indexPath withValue:[NSString stringWithFormat:@"%@",StrQuantity]];

    }

    - (void)updateCell:(NSIndexPath *)indexpathValue withValue:(NSString *)value
    {
        NSDictionary* dict = [Array_That_maintain_Count_Values objectAtIndex:indexpathValue.row];
        [dict setValue:value forKey:@"Product_Count"];
        [Array_That_maintain_Count_Values replaceObjectAtIndex:indexpathValue.row withObject:dict];

        [tbl reloadRowsAtIndexPaths:@[indexpathValue] withRowAnimation:UITableViewRowAnimationFade];

    }
现在,维护计数值的数组将包含每个节点的点击次数。 希望它能帮助你

现在,维护计数值的数组将包含每个节点的点击次数。
希望它能帮助你

首先,您需要有一个变量来存储计数。这取决于您希望变成这样的行数。如果有多个数组,则需要像
[Int]
这样的本地数组。假设您在第0节第0行有一个单元格需要此功能,其初始值为5。所以你会有

var count = 5

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell

    cell.label.text = String(count)

    return cell
}
现在需要处理单元格单击事件。单击单元格时,您希望首先增加此计数,然后重新加载单元格,以便触发
cellForRow
以更新标签。所以你需要

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.tableView.deselectRow(at: indexPath, animated: true)

    if indexPath.row == 0 && indexPath.section == 0 {
        self.count ++
        self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .none)
    }
}

首先,您需要有一个变量来存储计数。这取决于您希望变成这样的行数。如果有多个数组,则需要像
[Int]
这样的本地数组。假设您在第0节第0行有一个单元格需要此功能,其初始值为5。所以你会有

var count = 5

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell

    cell.label.text = String(count)

    return cell
}
现在需要处理单元格单击事件。单击单元格时,您希望首先增加此计数,然后重新加载单元格,以便触发
cellForRow
以更新标签。所以你需要

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.tableView.deselectRow(at: indexPath, animated: true)

    if indexPath.row == 0 && indexPath.section == 0 {
        self.count ++
        self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .none)
    }
}

您必须维护一个局部变量,以保存上次计数与新计数进行比较,并在每次单击时添加观察者。您必须更新到数据中。您可以将值存储并在自定义单元格类中递增,也可以将数据存储在view controller类中,递增,然后重新加载单元格。使用ReloadRowatineXpath重新加载单元格将调用CellForRowatineXpath,这将再次重置我的标签文本,当行在重新加载时设置动画时,它只显示我更新的文本一秒钟,然后再次将其重置为以前的值。您必须维护一个局部变量以保存上次计数与新计数进行比较,并在每次单击时添加观察者。您必须更新到数据中。您可以将值存储并在自定义单元格中递增类,或者您可以将数据存储在视图控制器类中,增加数据量,然后重新加载单元格。使用reloadrowatindexpath重新加载单元格将调用cellforrowatindexpath,这将再次重置我的标签文本,当行在重新加载时设置动画时,它只显示我更新的文本一秒钟,然后再次将其重置为以前的值。只有当我在该视图上时,每当我单击时,它应该递增row@Trapti你的工作做完了吗??根据您的情况,代码运行良好?只有当我在该视图上时,只要我单击row@Trapti你的工作做完了吗??根据您的案例,代码运行正常吗?