Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get indexPath of UITableViewCell on click of Button from Cell

Please Look at this image

I have a button (red color cross) in the UITableViewCell and on click of that button I want to get indexPath of the UITableViewCell.

Right now I am assigning tag to each of the button like this cell.closeButton.tag = indexPath.section and the on click of the button I get the indexPath.section value like this:

@IBAction func closeImageButtonPressed(sender: AnyObject) {
  data.removeAtIndex(sender.tag)
  tableView.reloadData()
}

Is this the right way of implementation or is there any other clean way to do this?

like image 203
Anirudha Mahale Avatar asked Sep 20 '16 04:09

Anirudha Mahale


People also ask

How do you get the indexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

What is indexPath section?

Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section. For example, the first row in a table would have section 0, row 0, whereas the eighth row in the fourth section would have section 3, row 7.

How do I create an indexPath in Objective C?

To create an IndexPath in objective C we can use. NSIndexPath *myIP = [NSIndexPath indexPathForRow: 5 inSection: 2] ; To create an IndexPath in Swift we can use.


2 Answers

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
    }
}
like image 57
Bista Avatar answered Oct 03 '22 22:10

Bista


How to get cell indexPath for tapping button in Swift 4 with button selector

@objc func buttonClicked(_sender:UIButton){

 let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView)
 let indexPath = self.tableView.indexPathForRow(at:buttonPosition)
 let cell = self.tableView.cellForRow(at: indexPath) as! UITableViewCell
 print(cell.itemLabel.text)//print or get item
}
like image 37
Saurabh Sharma Avatar answered Oct 03 '22 22:10

Saurabh Sharma