Seb Seb - 1 year ago 132
Swift Question

Get section number in custom cell button action // swift

I have a tableView dynamically populated with custom cells in several sections.

In my CustomCell class I have an @IBAction for a custom checkbox button in the cell. Is there a way to get the section number of the sender button in the IBAction function?

Something like:

@IBAction func checkboxButton(sender: CheckBox) {
//Change Button state
if sender.isChecked == true { sender.isChecked = false }
else { sender.isChecked = true }

//Get section number of clicked button
var sectionNumber = ???????




Answer Source

One of the ways, suppose you have custom cell and button inside a cell ... Usual pattern would be: (if you're using table view with datasource) when configuring the cell inside:

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath

you can set:

cell.button.tag = sectionIndex

So in:

func checkboxButton(sender: CheckBox) { ...

sender.tag would be sectionIndex

Hope you got the idea.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download