Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get section number in custom cell button action

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 = ???????

}
like image 467
Seb Avatar asked Dec 01 '22 15:12

Seb


1 Answers

You can easily find the row by calculating the position of the sender and finding the row at that position in the UITableView

var position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
if let indexPath = self.tableView.indexPathForRowAtPoint(position)
{
    let section = indexPath.section
    let row = indexPath.row
}

This is how Apple does it in the Accessory sample. https://developer.apple.com/library/ios/samplecode/Accessory/Listings/AccessoryViewController_m.html#//apple_ref/doc/uid/DTS40008066-AccessoryViewController_m-DontLinkElementID_4

like image 74
rakeshbs Avatar answered Dec 07 '22 00:12

rakeshbs