如何在cell.swift中获取indexPath.row

Sha*_*aek 25 uitableview nsindexpath swift

我有2个文件.

  • myTableViewController.swift
  • myTableCell.swift

我可以在myTabelCell.swift函数中获取indexPath.row吗?

这是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!

    }
Run Code Online (Sandbox Code Playgroud)

这是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



 }
Run Code Online (Sandbox Code Playgroud)

正如你所看到的...我正在尝试在myTableCell,liktBtnTapped函数中获取indexPath.row.

你能告诉我如何访问或获取IndexPath.row吗?

Cal*_*lam 33

我创建了一个UIResponder带有递归方法的扩展,您可以在任何UIView(继承自UIResponder)中使用它来查找特定类型的父视图.

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() })
    }
}
Run Code Online (Sandbox Code Playgroud)

使用这个,我们可以扩展UITableViewCell一些方便的只读计算属性,用于表格视图和单元格的索引路径.

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

    var indexPath: IndexPath? {
        return self.tableView?.indexPath(for: self)
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是您在示例中使用它的方法:

@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) }
}
Run Code Online (Sandbox Code Playgroud)

  • @Callam 不,最好不要将索引路径传递给单元格。这实际上是最糟糕的解决方案。如果可以在表视图中插入、删除或移动行,它就会失败。 (2认同)

Rak*_*tri 12

Swift 4+

在你的细胞内尝试这个.

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
}
Run Code Online (Sandbox Code Playgroud)


Tar*_*epp 8

容易..你可以在按钮动作中这样做:

let section = 0
let row = sender.tag
let indexPath = IndexPath(row: row, section: section)
let cell: myTableCell = self.feedTableView.cellForRow(at: indexPath) as! myTableCell
Run Code Online (Sandbox Code Playgroud)

然后在cellForRowAtIndexPath

// add the row as the tag
cell.button.tag = indexPath.row
Run Code Online (Sandbox Code Playgroud)


Rei*_*ill 6

Swift 4.2 的另一种方法,而不是假设 Superview 将始终是 tableview

extension UITableViewCell{

    var tableView:UITableView?{
        return superview as? UITableView
    }

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

}
Run Code Online (Sandbox Code Playgroud)

使用示例

@IBAction func checkBoxAction(_ sender: UIButton) {

        guard let indexPath = indexPath else { return }

        sender.isSelected = !sender.isSelected
        myCustomCellDelegate?.checkBoxTableViewCell(didSelectCheckBox: sender.isSelected, for: indexPath)

}
Run Code Online (Sandbox Code Playgroud)