LifeOS LifeOS - 6 months ago 23
Swift Question

Get data of row when checking check button Swift OSX

I made a table view with name, lastname and a check button. What I want is when someone hits that check button I want it to save the data of that checked row.

Image of tableview:

enter image description here

Answer

First set tag and target for that button in cellforrowatindex like,

cell.btn.tag = indexPath.row
cell.btn.addTarget(self, action: "getValue:", forControlEvents: UIControlEvents.TouchUpInside)

Then create one function to getValue,

@IBAction func getValue(sender : UIButton)
{
    //Here you have to set button image like selected etc 
    // Here you have to get data from your array or dictionary based on index path 
    let dicObj = self.allData.objectAtIndex(sender.tag) as NSMutableDictionary
    let firstName = dicObj.valueForKey("fname") as String
    let lastName = dicObj.valueForKey("lname") as String

    //Save records as you want it- your code here 
}

Hope this will work for you.