How to Get Indexpath.Row in Cell.Swift

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.

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<U: UIResponder>(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) }
}

Get indexPath.row in cell

just copy the given extension

extension UITableView {    
func indexPath(forItem: AnyObject) -> IndexPath? {
let itemPosition: CGPoint = forItem.convert(CGPoint.zero, to: self)
return self.indexPathForRow(at: itemPosition)
}}

USE:

@IBAction func cellPlayButtonPressed(_ sender: Any) {
if let indexPath = runnerTableView.indexPath(forItem: sender) {
print(indexPath)
}
}

How to get indexPath from image gesture in table view's cell

Well, you need to change your code little bit instead of gesture.view you need to find location in your tableView

@objc func imageTapped(_ gesture:UITapGestureRecognizer){

guard let indexPath = myTableView.indexPathForRow(at: gesture.location(in: self.tableView)) else {
print("Error: indexPath)")
return
}

print("indexPath.row: \(indexPath.row)")

}

and if problem still persists, you can give tag to imageView like this

imageview.tag = indexPath.row //place it in cellForRow

//place below lines in gesture handler
guard let view = sender.view else {return}
let indexPath = IndexPath(row:view.tag, section: 0) //only works if section remains same/ unique

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 Indexpath of textfield which placed in tableview when tap on done button using keyboard IQKeyboardManagerSwift in swift

try this code, just copy and paste

 set UITextFieldDelegate, UITextViewDelegate 

var strViewFooter = ""
var textFieldIndexPath = IndexPath()

func numberOfSections(in tableView: UITableView) -> Int {
arraycheck.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
1
}
func textViewDidEndEditing(_ textView: UITextView) {
strViewFooter = textView.text
}
func textFieldDidEndEditing(_ textField: UITextField) {

let cell: UITableViewCell = textField.superview?.superview as! UITableViewCell
let table: UITableView = cell.superview as! UITableView
textFieldIndexPath = table.indexPath(for: cell)!
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TimeSheetTableViewCell", for: indexPath) as? TimeSheetTableViewCell else {
return UITableViewCell()
}
cell.txtTimeSheet.keyboardToolbar.doneBarButton.setTarget(self, action: #selector(doneButtonClicked))
return cell
}

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let vw = UIView()
vw.backgroundColor = UIColor.clear
let titleLabel = UITextView(frame: CGRect(x:10,y: 5 ,width:350,height:150))
titleLabel.backgroundColor = UIColor.clear
titleLabel.font = UIFont(name: "Montserrat-Regular", size: 12)
titleLabel.text = arrayFootter[section]
titleLabel.tag = section
vw.addSubview(titleLabel)
titleLabel.keyboardToolbar.doneBarButton.setTarget(self, action: #selector(donebuttonClickedOnView))
return vw
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 50
}

@objc func doneButtonClicked(_ sender: Any) {
print(textFieldIndexPath)
print(textFieldIndexPath.row)
print(textFieldIndexPath.section)

}
@objc func donebuttonClickedOnView(_ sender: UIBarButtonItem){
arrayFootter.remove(at: sender.tag)
arrayFootter.insert(strViewFooter, at: sender.tag)
print(sender.tag)
}


Related Topics



Leave a reply



Submit