Get Section Number in Custom Cell Button Action

Get section number in custom cell button action

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.

get section number and row number on custom cells button click?

You'll need to implement a UIControl event-handling method on your view controller, and set that as the handler for all your buttons. i.e. inside your -tableView:cellForRowAtIndexPath: function you would do something like:

[theCell.button addTarget: self
action: @selector(buttonPressed:withEvent:)
forControlEvents: UIControlEventTouchUpInside];

Then your event handler would look like this:

- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
UITouch * touch = [[event touches] anyObject];
CGPoint location = [touch locationInView: self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];

/* indexPath contains the index of the row containing the button */
/* do whatever it is you need to do with the row data now */
}

Find Out Which Section Button Belongs To In UITableView When Clicked Inside Cell

If you only need the section (not the section and row) of the index path of the cell containing the tapped button, you could tag the button with the section number (Int) when you configure the table view cell:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier", forIndexPath:indexPath) as! MyCustomTableViewCell

cell.myCustomButton.tag = indexPath.section

// Remove all existing targets (in case cell is being recycled)
cell.myCustomButton.removeTarget(nil, action: nil, forControlEvents: .AllEvents)

// Add target
cell.myCustomButton.addTarget(self, action:"buttonAction:", forControlEvents:.TouchUpInside)

return cell
}

func buttonAction(sender:AnyObject)
{
if let button = sender as UIButton{
println("Tapped Button in section: \(button.tag)")
}
}

If you need both the section AND the row, you're better off storing the whole index path. For that, you can either use a custom UIButton subclass with a property of type NSIndexPath, or store it in the table view cell.
But the second solution is a bit messy since you have to access the table cell (and then, the index path) from the button by using superview, etc., and this relies on the table cell's subview structure. I'm not sure if the parent view of your button is the table cell itself, or its "content view" (I'm a bit rusty right now...)

How to know the section number on button click of Tableview cell in a UITableView?

Create a handler for button click and add it in tableView:cellForRowAtIndexPath: method

- (void)buttonPressed:(UIButton *)button{

UITableViewCell *cell = button.superView.superView;

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
//Now you have indexPath of the cell
//do your stuff here

}

get section cell Text on custom cells button click?

When you are drawing the cell set the tag on the button as an index into the array of data that you are displaying

[acceptAnswersButton setTag:indexPath.row]

Then when the button is clicked you can get the tag from the action that is called as the button will be passed as a parameter.


If you add the tag to the button for each row. Then in the associated action you have the index into the array that you got the text from to retrieve it again.

arrayText = {"one", "two", "three"}

This means that the cellForRowAtIndexPath will set the tag for each button as

row1ButtonTag=0
row2ButtonTag=1
row3ButtonTag=2

and when the action is called you will have access to the button that called the action and therefore access to the index into the array

(IBAction)myAction:(id)sender {
NSString *theTextIWasLookingFor = [arrayText objectAtIndex:((UIButton*)sender).tag];
}

get indexPath of UITableViewCell on click of Button from Cell

Use Delegates:

MyCell.swift:

import UIKit

//1. delegate method
protocol MyCellDelegate: AnyObject {
func btnCloseTapped(cell: MyCell)
}

class MyCell: UICollectionViewCell {
@IBOutlet var btnClose: UIButton!

//2. create delegate variable
weak var delegate: MyCellDelegate?

//3. assign this action to close button
@IBAction func btnCloseTapped(sender: AnyObject) {
//4. call delegate method
//check delegate is not nil with `?`
delegate?.btnCloseTapped(cell: self)
}
}

MyViewController.swift:

//5. Conform to delegate method
class MyViewController: UIViewController, MyCellDelegate, UITableViewDataSource,UITableViewDelegate {

//6. Implement Delegate Method
func btnCloseTapped(cell: MyCell) {
//Get the indexpath of cell where button was tapped
let indexPath = self.collectionView.indexPathForCell(cell)
print(indexPath!.row)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as! MyCell
//7. delegate view controller instance to the cell
cell.delegate = self

return cell
}
}

UITableViewCell Buttons with action

I was resolving this using a cell delegate method within UITableViewCell's subclass.

Quick overview:

1) Create a protocol

protocol YourCellDelegate : class {
func didPressButton(_ tag: Int)
}

2) Subclass your UITableViewCell (if you haven't done so):

class YourCell : UITableViewCell
{
var cellDelegate: YourCellDelegate?
@IBOutlet weak var btn: UIButton!
// connect the button from your cell with this method
@IBAction func buttonPressed(_ sender: UIButton) {
cellDelegate?.didPressButton(sender.tag)
}
...
}

3) Let your view controller conform to YourCellDelegate protocol that was implemented above.

class YourViewController: ..., YourCellDelegate {  ... }

4) Set a delegate, after the cell has been defined (for reusing).

let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! YourCell
cell.cellDelegate = self
cell.btn.tag = indexPath.row

5) In the same controller (where is your implemented UITableView delegate/datasource), put a method from YourCellDelegate protocol.

func didPressButton(_ tag: Int) {
print("I have pressed a button with a tag: \(tag)")
}

Now, your solution is not tag / number dependent. You can add as many buttons as you want, so you are ready to get response via delegate regardless how many buttons you want to install.

This protocol-delegate solution is preferred in iOS logic and it can be used for other elements in table cell, like UISwitch, UIStepper, and so on.



Related Topics



Leave a reply



Submit