首页 > 解决方案 > UITableView 滑动操作删除和编辑自定义视图

问题描述

我在滑动表格视图单元格时设置视图,就像表格中的第一个视图一样,它需要作为第二个视图粘贴并更改其颜色。

可以在 Swift 4 和 iOS 中使用吗?

在此处输入图像描述

标签: iosswiftuitableview

解决方案


在视图控制器中获得滑动手势后,您将执行以下操作:

func didSwipe(_ cell: UITableViewCell) {
   let indexPath = tableView.indexPath(for: cell)
   if indexPath.row + 1 < myViewModels.count - 1 {
     // Store the info from the next view model to the current one
     myViewModels[indexPath.row].update(with: myViewModels[indexPath.row + 1])
     tableView.reloadItems(at: [indexPath])
   }
}

myViewModels 是您正在显示的对象数组(包含数据、颜色等),您可以手动更新必要的字段而不是更新

代码可能有错误,但思路应该很清楚


推荐阅读