0
votes

I have a

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

I tried to set custom action for cell in UITableView depending on IndexPath.row.

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:

The problem occurs with cells, which I want to be without actions (which corresponding items has .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

1

1 Answers

0
votes

I found a solution: just use canEditRowAt method of UITableView

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