简体   繁体   English

如何根据IndexPath.row设置自定义UITableViewRowAction?

[英]How to set custom UITableViewRowAction depending on IndexPath.row?

I have a 我有一个

  • tableView(UITableView) tableView(UITableView)
  • customCell: UITableViewCell! customCell:UITableViewCell!
  • itemsArray: [Item] itemsArray:[Item]

I tried to set custom action for cell in UITableView depending on IndexPath.row. 我试图根据IndexPath.row为UITableView中的单元格设置自定义操作。

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath)
 -> [UITableViewRowAction]? {

  if !(itemsArray[indexPath.row].isReadOnly) {

    let editAction = UITableViewRowAction(style: .normal, title: "Edit") {
       (tableViewRowAction, indexPath) in
       print("Edit Item \(self.itemsArray[indexPath.row].caption)\n")
    } 
    return [editAction]
  } else {
    return []
  }
}

I tried to set custom action for cell in UITableView depending on IndexPath.row: 我试图根据IndexPath.row为UITableView中的单元格设置自定义操作:

The problem occurs with cells, which I want to be without actions (which corresponding items has .isReadOnly = true) 问题出现在单元格上,我希望它不执行任何操作(相应的项目具有.isReadOnly = true)

I tried to return nil and [] in else case and both variants has irrelevant result for swiping: - nil – Delete action shows for items - [] – Cell swipes a little bit, “unswipes” back an than swipes stops working in any cell 在其他情况下,我尝试返回nil和[],并且两种变体的刷卡结果都不相关:-nil-删除项目的显示操作-[]-单元格滑动了一点,“取消滑动”了一个,而在任何单元格中滑动都停止了

I found a solution: just use canEditRowAt method of UITableView 我找到了解决方案:只需使用UITableView的canEditRowAt方法

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return !(items[indexPath.row].isReadOnly)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM