I have an app that has a custom button in a custom cell. If you select the cell it segues to the a detail view, which is perfect. If I select a button in a cell, the code below prints the cell index into the console.
I need to access the contents of the selected cell (Using the button) and add them to an array or dictionary. I am new to this so struggling to find out how to access the contents of the cell. I tried using didselectrowatindexpath, but I don't know how to force the index to be that of the tag...
So basically, if there are 3 cells with 'Dog', 'Cat', 'Bird' as the cell.repeatLabel.text in each cell and I select the buttons in the rows 1 and 3 (Index 0 and 2), it should add 'Dog' and 'Bird' to the array/dictionary.
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postsCollection.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
// Configure the cell...
var currentRepeat = postsCollection[indexPath.row]
cell.repeatLabel?.text = currentRepeat.product
cell.repeatCount?.text = "Repeat: " + String(currentRepeat.currentrepeat) + " of " + String(currentRepeat.totalrepeat)
cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton
cell.checkButton.tag = indexPath.row;
cell.checkButton.addTarget(self, action: Selector("selectItem:"), forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func selectItem(sender:UIButton){
println("Selected item in row \(sender.tag)")
}
XCODE 8: Important Note
The fix is to set the tags to different values. Hope it helps someone.
Since you have 1 section in the table view you can get the cell object as below.
where tag you will get from button tag.
OPTION 1. Handling it with delegation
The right way of handling events fired from your cell's subviews is to use delegation.
So you can follow the steps:
1. Above your class definition write a protocol with a single instance method inside your custom cell:
2. Inside your class definition declare a delegate variable and call the protocol method on the delegate:
3. Conform to the CustomCellDelegate in the class where your table view is:
4. Set your cell's delegate
5. Implement the required method in your view controller class.
EDIT: First define an empty array and then modify it like this:
where items is an array defined in my view controller:
OPTION 2. Handling it using closures
I've decided to come back and show you another way of handling these type of situations. Using a closure in this case will result in less code and you'll achieve your goal.
1. Declare a closure variable inside your cell class:
2. Invoke the closure inside your button handler.
3. In
tableView(_:cellForRowAtIndexPath:)
in the containing view controller class :I updated option 1 of the answer from Vasil Garov for Swift 3
1. Create a protocol for your
CustomCell
:2. For your
TableViewCell
declare adelegate
variable and call the protocol method on it:3. Conform to the
CustomCellDelegate
in the class where yourtableView
is:4. Set your cell's
delegate
5. Implement the required method in your
ViewController
.Swift 3 I just get solution for access cell in @IBAction function using superview of button tag.