Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get indexPath.row in cell.swift

I have 2 files.

  • myTableViewController.swift
  • myTableCell.swift

Can I get the indexPath.row in myTabelCell.swift function?

Here is myTableCell.swift

import UIKit import Parse import ActiveLabel  class myTableCell : UITableViewCell {      //Button     @IBOutlet weak var commentBtn: UIButton!     @IBOutlet weak var likeBtn: UIButton!     @IBOutlet weak var moreBtn: UIButton!       override func awakeFromNib() {         super.awakeFromNib()       }      @IBAction func likeBtnTapped(_ sender: AnyObject) {          //declare title of button         let title = sender.title(for: UIControlState())          //I want get indexPath.row in here!      } 

Here is myTableViewController.swift

class myTableViewController: UITableViewController {      //Default func     override func viewDidLoad() {         super.viewDidLoad()          //automatic row height         tableView.estimatedRowHeight = 450         tableView.rowHeight = UITableViewAutomaticDimension        }   // cell config     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {           //define cell         let cell = tableView.dequeueReusableCell(withIdentifier: "myTableCell", for: indexPath) as! myTableCell     } 

As you can see... I'm trying to get indexPath.row in myTableCell, liktBtnTapped function.

Could you let me know how can I access or get IndexPath.row?

like image 338
Shawn Baek Avatar asked Nov 05 '16 11:11

Shawn Baek


People also ask

How do I get last IndexPath in Swift?

You can get the indexPath of the last row in last section like this. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(numberOfRowsInLastSection - 1) inSection:(numberOfSections - 1)]; Here, numberOfSections is the value you return from numberOfSectionsInTableView: method.

What is IndexPath row in Swift?

In Swift, an indexPath is a list of indexes that, together, represent the path to a specific location in a tree of nested arrays. It describes an item's position inside a table view or collection view, storing both its section and its position inside that section.

What does IndexPath row return?

row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.


2 Answers

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) } } 
like image 110
Callam Avatar answered Nov 04 '22 21:11

Callam


Swift 4+

Try this inside your cell.

func getIndexPath() -> IndexPath? {     guard let superView = self.superview as? UITableView else {         print("superview is not a UITableView - getIndexPath")         return nil     }     indexPath = superView.indexPath(for: self)     return indexPath } 
like image 35
Rakesha Shastri Avatar answered Nov 04 '22 20:11

Rakesha Shastri