I have a view controller with a table view and a separate nib for the table cell template. The cell template has some buttons. I want to access the button click along with the index of the cell clicked inside the view controller where I have defined the Table view.
So I have ViewController.h
and ViewController.m
where I have the UITableView
and TableTemplate.h
, TableTemplate.m
and TableTemplate.xib
where I have the nib defined. I want the button click event with cell index in ViewController.m
.
Any help on how can I do that?
The reason i like below technique because it also help me to identify the section of table.
Add Button in cell cellForRowAtIndexPath:
Event addTask:
Hopes this help.
Following code might Help you.
I have taken
UITableView
with custom prototype cell class namedUITableViewCell
insideUIViewController
.So i have
ViewController.h
,ViewController.m
andTableViewCell.h
,TableViewCell.m
Here is the code for that:
ViewController.h
ViewController.m
Custom cell class :
and
Note: Here I have taken all
UIControls
using Storyboard.Hope that can help you...!!!
Swift 2.2
You need to add target for that button.
FunctionName: connected // for example
And of course you need to set tag of that button since you are using it.
You can achieve this by subclassing UITableViewCell. Use it in interface builder, drop a button on that cell, connect it via outlet and there you go.
To get the tag in the connected function:
Delegates are the way to go.
As seen with other answers using views might get outdated. Who knows tomorrow there might be another wrapper and may need to use
cell superview]superview]superview]superview]
. And if you use tags you would end up with n number of if else conditions to identify the cell. To avoid all of that set up delegates. (By doing so you will be creating a re usable cell class. You can use the same cell class as a base class and all you have to do is implement the delegate methods.)First we need a interface (protocol) which will be used by cell to communicate(delegate) button clicks. (You can create a separate .h file for protocol and include in both table view controller and custom cell classes OR just add it in custom cell class which will anyway get included in table view controller)
Include this protocol in custom cell and table view controller. And make sure table view controller confirms to this protocol.
In custom cell create two properties :
In
UIButton
IBAction delegate click : (Same can be done for any action in custom cell class which needs to be delegated back to view controller)In table view controller
cellForRowAtIndexPath
after dequeing the cell, set the above properties.And implement the delegate in table view controller:
This would be the ideal approach to get custom cell button actions in table view controller.
This should help :-
Here sender is the UIButton instance that is sending the event. myTableView is the UITableView instance you're dealing with.
Just get the cell reference right and all the work is done.
You may need to remove the buttons from cell's contentView & add them directly to UITableViewCell instance as it's subview.
Or
You can formulate a tag naming scheme for different UIButtons in cell.contentView. Using this tag, later you can know the row & section information as needed.