ios 从 Swift 中的 UITableViewCell 子类中获取 IndexPath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25867439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:29:14  来源:igfitidea点击:

Get the IndexPath from inside a UITableViewCell subclass in Swift

iosuitableviewswiftuibuttonnsindexpath

提问by Isuru

I have a custom UITableViewCellsubclass and its associated xib. I have a UILabeland a UIButtonin this cell and I have wired the touch up inside action of the button to the subclass.

我有一个自定义UITableViewCell子类及其关联的 xib。我在这个单元格中有一个UILabel和一个UIButton,并且我已经将按钮的内部操作连接到子类。

What I need is when that button in the cell is tapped, to get the indexpath of the cell which has that button. And maybe send it back to the view controller via a delegate or something.

我需要的是当单元格中的那个按钮被点击时,获取具有该按钮的单元格的索引路径。并且可能通过委托或其他方式将其发送回视图控制器。

Since I'm inside a UITableViewCellsubclass, I can't use a solution like thisbecause I don't have a reference to the tableview from inside the cell subclass. Upon further investigation I found anothersolution and I implemented it in Swift like this.

因为我是里面UITableViewCell的子类,我不能使用的溶液等这个,因为我不从小区子类中,必须在tableview中的参考。经过进一步调查,我找到了另一个解决方案,并像这样在 Swift 中实现了它。

import UIKit

class ContactCell: UITableViewCell {

    @IBOutlet weak var nameLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        selectionStyle = .None
    }

    @IBAction func callButtonPressed(sender: UIButton) {
        let indexPath = (self.superview as UITableView).indexPathForCell(self)
        println("indexPath?.row")
    }

}

But when I tap on the button, it crashes with an error message saying Swift dynamic cast failed.

但是当我点击按钮时,它会崩溃并显示一条错误消息,指出Swift dynamic cast failed

Any idea what's wrong with my code?

知道我的代码有什么问题吗?

Or I'm open to any other suggestions which would allow me to achieve the desired result in any other way.

或者我愿意接受任何其他建议,这些建议可以让我以任何其他方式实现预期的结果。

Thank you.

谢谢你。

回答by ullstrm

Sounds like you need a delegate:

听起来你需要一个代表:

Delegates in swift?

迅速代表?

Then just pass the cell itself as a parameter to the delegate, and then you can easily do tableView.indexPathForCell(cellFromDelegateMethod)

然后只需将单元格本身作为参数传递给委托,然后您就可以轻松做到 tableView.indexPathForCell(cellFromDelegateMethod)

回答by Sibasish

Hey you can use "Tag" of the button also.Inside the cellForRowAt method of table delegate u can tag the button with Indexpath.row . here is the example what i m tried to say.

嘿,您也可以使用按钮的“标记”。在表委托的 cellForRowAt 方法中,您可以使用 Indexpath.row 标记按钮。这是我试图说的例子。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// get ur cell nib .As it has a button
cell.startOrConntinuBtn.addTarget(self, action: #selector(sumbitOrContinue), for: .touchUpInside) 

cell.startOrConntinuBtn.tag = indexPath.row }

and in the touch method "sumbitOrContinue" -

并在触摸方法“sumbitOrContinue”中 -

 func sumbitOrContinue(sender: UIButton!) { 
let tag = sender.tag
// do what you want to do like this example

let detail = self.detailList[tag]
let vc  = self.storyboard?.instantiateViewController(withIdentifier: "mockExamInt") as! MockWindowVc
vc.detailId = detail.id
self.navigationController?.pushViewController(vc, animated: true)}

回答by Эльвира Чекменева

UIButton.Type really does not have member superview, but sender have

UIButton.Type 确实没有成员 superview,但是 sender 有

var cell: UITableViewCell = sender.superview.superview as UITableViewCell

var cell: UITableViewCell = sender.superview.superview as UITableViewCell