关于TableViewIndexPath的注意点

公司产品有个功能是cell右边放了一个switch控件,在操作状态的时候通过网络请求来改变最后的状态。

效果图

实现代码如下:

- (UITableViewCell *)lh_tableView:(LHBaseTableView *)baseView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    @weakify(self)
    cell.cellOperationHandler = ^(LHBaseTableViewCell *eventCell, UIView *sender, NSObject *info, void (^completion)(NSError *)) {
        NSIndexPath *cellIndexPath = [baseView indexPathForCell:eventCell];
        LHLinkage *linkage = weak_self.viewModel.linkageGroupList[indexPath.row];
        [linkage changeLinakgeStatusCompletion:^(BOOL isSuccess, NSError *error) {
            if (error) {
                [LHTipsUtils showError:error];
            }
            if (completion) {
                completion(error);
            }
        }];
    };
...
}

- (void)onSwitchValueChange:(UISwitch *)sender{
   if(self.cellOperationHandler){
        sender.enabled = false;
        __weak typeof(self) weakself = self;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            BOOL oldValue = !sender.isOn;
            weakself.cellOperationHandler(weakself, sender, [NSNumber numberWithBool:sender.isOn], ^(NSError *error) {
                sender.enabled = true;
                if (error) {
                    [sender setOn:oldValue animated:true];
                }
            });
        });
    }
}

然后测试妹纸反馈说有时候状态不对,查了下代码开始发现没有什么不对的啊,我们知道tableView:(LHBaseTableView *)baseView cellForRowAtIndexPath:(NSIndexPath *)indexPath因为重用关系,indexPath是不可靠的,但是这里是用的indexPathForCell:方法,为啥也会出问题?

然后写了测试代码:


image.png

在cell上Switch执行的时候,快速的滑动tableView
然后控制台打印结果:

Printing description of test:
<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
Printing description of indexPath:
<NSIndexPath: 0xc000000001200016> {length = 2, path = 0 - 9}

发现这时候确实是发生变化了。
那这里最好的办法就是把当前cell操作的indexPath给存起来,然后操作完毕后给到外面,就是把数据源和UI操作分开。
改动后的代码:

- (void)onSwitchValueChange:(UISwitch *)sender{
    if(self.cellOperationHandler){
        sender.enabled = false;
        __weak typeof(self) weakself = self;
        NSIndexPath *indexPath;
        if ([self.superview isKindOfClass:[UITableView class]]) {
            indexPath = [((UITableView *)self.superview) indexPathForCell:self];
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            BOOL oldValue = !sender.isOn;
            weakself.cellOperationHandler(weakself, indexPath, sender, [NSNumber numberWithBool:sender.isOn], ^(NSError *error) {
                sender.enabled = true;
                if (error) {
                    [sender setOn:oldValue animated:true];
                }
            });
        });
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容