Search code examples
swiftuitableviewuitableviewrowaction

Swift UITableViewRowAction different number of buttons on different rows


With the UITableViewRowAction can you have different number of buttons on each row? For instance I want one row with 3 actions and the rest with 2. Is it possible?

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

if indexPath.row == 0 {
  let note = UITableViewRowAction(style: .Default, title: " Note ") { action, index in

    self.notePopUpCalled()
  }
  note.backgroundColor = GlobalColourConstants.informationColour

  let infomation = UITableViewRowAction(style: .Default, title: " Info ") { action, index in

    self.informationPopUpCalled()
  }
  infomation.backgroundColor = GlobalColourConstants.appColour

let Diagram = UITableViewRowAction(style: .Default, title: " Diagram ") { action, index in

    self.diagramPopUpCalled()
  }
  note.backgroundColor = GlobalColourConstants.informationColour
  return [note, information, diagram]


} else {
  let note = UITableViewRowAction(style: .Default, title: " Note ") { action, index in

  }
  note.backgroundColor = GlobalColourConstants.informationColour

  let infomation = UITableViewRowAction(style: .Default, title: " Info ") { action, index in

  }
  infomation.backgroundColor = GlobalColourConstants.appColour
  return [note, information, nil]
 }
}

Solution

  • Yes this is possible. The method:

    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
    

    returns an array of actions for each index path. Therefore you can return different set of actions for each row.