关于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:
{length = 2, path = 0 - 0}
Printing description of indexPath:
{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];
                }
            });
        });
    }
}

你可能感兴趣的:(关于TableViewIndexPath的注意点)