ios - 在 UiTableView didSelectRowAtIndexPath 更新错误的 Cell

标签 ios objective-c uitableview didselectrowatindexpath

在我的 TableView 中,didSelectRowAtIndexPath 仅更新每个单元格上表的最后一个可见单元格是选择。

示例代码如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:subcategoryTV])
    {
         selCell=(MDTableViewCell *)[subcategoryTV cellForRowAtIndexPath:indexPath ];

         if ([checkButton.titleLabel.text isEqualToString:@"✓"])
         {
            checkButton.backgroundColor=[UIColor clearColor] ;
            [checkButton setTitle:resetTitle forState:UIControlStateNormal];
            [checkButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
            checkButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        }
        else
        {
            resetTitle=checkButton.titleLabel.text;
            NSLog(@"%@",resetTitle);

            checkButton.backgroundColor=[UIColor colorWithRed:1 green:0.839 blue:0.314 alpha:1] ;
            [checkButton setTitle:@"✓" forState:UIControlStateNormal];
            [checkButton.titleLabel setFont:[UIFont fontWithName:@"Futura" size:25]];
            [checkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            checkButton.layer.borderColor = [UIColor clearColor].CGColor; 
        }

        if (costLabel.hidden==YES) {
            costLabel.hidden=NO;
        }
        else
        {
            costLabel.hidden=YES;
        }
     }
 }

最佳答案

无论如何,您不应该从单元格中查询数据,您应该检查您的模型以决定要做什么。当用户更改任何行或按钮时,您更新模型,然后使用它来更新 View 。这是正确的做法。

你这里的具体问题是你的错误方法是你在选定的索引路径上获取单元格然后不使用它。您应该从单元格中获取按钮和标签。

关于ios - 在 UiTableView didSelectRowAtIndexPath 更新错误的 Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34961158/

相关文章:

ios - 100个UIButton背景图片的最佳性能解决方案

ios - Flutter iOS 应用程序从 ipa 文件启动时崩溃,但从 xCode 运行时运行正常

javascript - 从某个网页上托管的 JavaScript 调用 Objective-C 函数

将 NSFetchedResultsControllerDelegate 与核心数据一起使用时,iOS Swift 的 didSelectRowAtIndexPath 方法未触发?

iOS 底部对齐 TableView 中的 TableViewController (StoryBoard)

ios - 使用参数在 iOS 中发出 HTTP Get 请求

ios - 如何使用 Objectivec 文件和 Swift 文件创建框架

objective-c - 在 XIB 或代码中构建 UITableViewCell?

iphone - 当 modalPresentationStyle 设置为 UIModalPresentationCurrentContext 时没有动画

ios - 未调用自定义 UITableViewCell didSelectRowAtIndexPath