Get Indexpath of Uitableviewcell on Click of Button from Cell

How to get the indexpath.row when an element is activated?

giorashc almost had it with his answer, but he overlooked the fact that cell's have an extra contentView layer. Thus, we have to go one layer deeper:

guard let cell = sender.superview?.superview as? YourCellClassHere else {
return // or fatalError() or whatever
}

let indexPath = itemTable.indexPath(for: cell)

This is because within the view hierarchy a tableView has cells as subviews which subsequently have their own 'content views' this is why you must get the superview of this content view to get the cell itself. As a result of this, if your button is contained in a subview rather than directly into the cell's content view, you'll have to go however many layers deeper to access it.

The above is one such approach, but not necessarily the best approach. Whilst it is functional, it assumes details about a UITableViewCell that Apple have never necessarily documented, such as it's view hierarchy. This could be changed in the future, and the above code may well behave unpredictably as a result.

As a result of the above, for longevity and reliability reasons, I recommend adopting another approach. There are many alternatives listed in this thread, and I encourage you to read down, but my personal favourite is as follows:

Hold a property of a closure on your cell class, have the button's action method invoke this.

class MyCell: UITableViewCell {
var button: UIButton!

var buttonAction: ((Any) -> Void)?

@objc func buttonPressed(sender: Any) {
self.buttonAction?(sender)
}
}

Then, when you create your cell in cellForRowAtIndexPath, you can assign a value to your closure.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! MyCell
cell.buttonAction = { sender in
// Do whatever you want from your button here.
}
// OR
cell.buttonAction = buttonPressed(closure: buttonAction, indexPath: indexPath) // <- Method on the view controller to handle button presses.
}

By moving your handler code here, you can take advantage of the already present indexPath argument. This is a much safer approach that the one listed above as it doesn't rely on undocumented traits.

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
}
}

How to get the indexPath.row when I click a button on a table view cell?

A common, generalized solution for this type of problem is to connect the @IBAction of the button to a handler in the cell (not in the view controller), and then use a delegate-protocol pattern so the cell can tell the table when the button was tapped. The key is that when the cell does this, it will supply a reference to itself, which the view controller can then use to determine the appropriate indexPath (and thus the row).

For example:

  • Give your UITableViewCell subclass a protocol:

    protocol CustomCellDelegate: class {
    func cell(_ cell: CustomCell, didTap button: UIButton)
    }
  • Hook up the @IBAction to the cell (not the view controller) and have that call the delegate method:

    class CustomCell: UITableViewCell {
    weak var delegate: CustomCellDelegate?

    @IBOutlet weak var customLabel: UILabel!

    func configure(text: String, delegate: CustomCellDelegate) {
    customLabel.text = text
    self.delegate = delegate
    }

    @IBAction func didTapButton(_ button: UIButton) {
    delegate?.cell(self, didTap: button)
    }
    }
  • Obviously, when the cell is created, call the configure method, passing, amongst other things, a reference to itself as the delegate:

    extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { ... }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
    let text = ...
    cell.configure(text: text, delegate: self)
    return cell
    }
    }
  • Finally, have the delegate method call indexPath(for:) to determine the index path for the cell in question:

    extension ViewController: CustomCellDelegate {
    func cell(_ cell: CustomCell, didTap button: UIButton) {
    guard let indexPath = tableView.indexPath(for: cell) else { return }

    // use `indexPath.row` here
    }
    }

The other approach is to use closures, but again using the same general pattern of hooking the button @IBAction to the cell, but have it call a closure instead of the delegate method:

  • Define custom cell with closure that will be called when the button is tapped:

    class CustomCell: UITableViewCell {
    typealias ButtonHandler = (CustomCell) -> Void

    var buttonHandler: ButtonHandler?

    @IBOutlet weak var customLabel: UILabel!

    func configure(text: String, buttonHandler: @escaping ButtonHandler) {
    customLabel.text = text
    self.buttonHandler = buttonHandler
    }

    @IBAction func didTapButton(_ button: UIButton) {
    buttonHandler?(self)
    }
    }
  • When the table view data source creates the cell, supply a handler closure:

    extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { ... }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
    let text = ...
    cell.configure(text: text, buttonHandler: { [weak self] cell in // the `[weak self]` is only needed if this closure references `self` somewhere
    guard let indexPath = tableView.indexPath(for: cell) else { return }

    // use `indexPath` here
    })
    return cell
    }
    }

I personally prefer the delegate-protocol pattern, as it tends to scale more nicely, but both approaches work.


Note, in both examples, I studiously avoided saving the indexPath in the cell, itself (or worse, “tag” values). By doing this, it protects you from getting misaligned if rows are later inserted and deleted from the table.


By the way, I used fairly generic method/closure names. In a real app, you might give them more meaningful names, e.g., didTapInfoButton, didTapSaveButton, etc.) that clarifies the functional intent.

How can I get indexPath.row in cell.swift

I have created a UIResponder extension with a recursive method that you can use in any UIView (which inherits from UIResponder) to find a parent view of a specific type.

import UIKit

extension UIResponder {
/**
* Returns the next responder in the responder chain cast to the given type, or
* if nil, recurses the chain until the next responder is nil or castable.
*/
func next(of type: U.Type = U.self) -> U? {
return self.next.flatMap({ $0 as? U ?? $0.next() })
}
}

Using this, we can extend UITableViewCell with some convenient read-only computed properties for the table view and index path of the cell.

extension UITableViewCell {
var tableView: UITableView? {
return self.next(of: UITableView.self)
}

var indexPath: IndexPath? {
return self.tableView?.indexPath(for: self)
}
}

Here is how you could use it in your example:

@IBAction func likeBtnTapped(_ sender: AnyObject) {
//declare title of button
let title = sender.title(for: UIControlState())

//I want get indexPath.row in here!
self.indexPath.flatMap { print($0) }
}

How to get the indexPath of a UIButton in a UITableViewCell?

Thanks to all i slove this by using below code

     NSIndexPath *indexPath = [panelTableView indexPathForCell:(UITableViewCell *)sender.superview.superview];

NSLog(@"%d",indexPath.row);

IOS 7 - How to get the indexPath from button placed in UITableViewCell

NSLog(@"%ld",(long)indexPath); is wrong this will print the pointer address of indexPath

try using following codes

CGPoint center= sender.center; 
CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.filterTableView];
NSIndexPath *indexPath = [self.filterTableView indexPathForRowAtPoint:rootViewPoint];
NSLog(@"%@",indexPath);


Related Topics



Leave a reply



Submit