ios - ReloadRows At IndexPath 导致更改背景图像出现问题

标签 ios objective-c xcode uitableview

我在右侧滑动时切换 UITableViewCell 上的背景图像。问题是如果我添加 [taskManager reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];它只会切换一次,然后不会切换回来。那么这是为什么呢?什么会让这不会发生?这是整个方法代码。

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer {
CGPoint location = [gestureRecognizer locationInView:self.taskManager];

NSIndexPath *indexPath = [self.taskManager indexPathForRowAtPoint:location];

[taskManager reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

UIImage *notCkDone = [UIImage imageNamed:@"UITableViewCellCheckmark"];
UIImage *isCkDone = [UIImage imageNamed:@"UITableViewCellCheckmarkDone"];

UIImage *notBgDone = [UIImage imageNamed:@"UITableViewCell"];
UIImage *isBgDone = [UIImage imageNamed:@"UITableViewCellDone"];

UITableViewCell *cell = [taskManager cellForRowAtIndexPath:indexPath];

if (indexPath) {
    if (cell.imageView.image == notCkDone) {
        cell.imageView.image = isCkDone;
        cell.backgroundView = [[UIImageView alloc] initWithImage:isBgDone];
    } else {
        cell.imageView.image = notCkDone;
        cell.backgroundView = [[UIImageView alloc] initWithImage:notBgDone];
    }
    cell.textLabel.textColor = [UIColor colorWithRed:0.0/0.0 green:0.0/0.0 blue:0.0/0.0 alpha:1.0];
}

}

最佳答案

对单元格视觉效果的所有更改都应在您的 tableView:cellForRowAtIndexPath: 内进行。 .您不应该在手势识别器的事件处理程序中执行此操作(或在 tableView:cellForRowAtIndexPath 之外的任何其他地方,就此而言)。

手势识别器需要更改表格 View 后面的模型,以便您的 tableView:cellForRowAtIndexPath 可以决定将图像设置为 notCkDoneisCkDone下次重新加载数据时,还要处理后台的变化。然后你的手势识别器应该告诉表格 View 重新加载数据。

您当前的解决方案在 tableView:cellForRowAtIndexPath 返回的单元格上运行.当您从外部更改视觉效果时,表格 View 会在单元格位于屏幕上时显示更改。但是,当单元格滚动到屏幕外时,它会被回收,这意味着下次您将具有单元格标识符的单元格出列时,您可能会得到一个用于显示为不同单元格的对象。

关于ios - ReloadRows At IndexPath 导致更改背景图像出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14513333/

相关文章:

iphone - 将 Controller 与界面构建器连接起来?

ios - 使用 ReactiveCocoa 时未调用错误 block

ios - 如何在 iOS 中获取唯一的设备 ID

SwiftUI:类型不符合协议(protocol) 'UIViewRepresentable'

objective-c - 加速度计和校准 - iPhone SDK

ios - UITableView 嵌套在 ContainerView 中不突出显示

ios - 长按模式下的 Collection View 时,如何移动 Collection View 单元格并在 iOS 中重新排列它们?

iphone - 隐藏导航栏以在界面生成器 iphone 中工作

ios - 如何在没有代码访问的情况下使用 GPX 文件在 IOS 真实设备上设置虚假 GPS 位置?

swift - 如何在 Swift 中更改 iOS 弹出窗口中箭头的背景颜色?