gpt4 book ai didi

ios - 找不到成员 indexPathForCell

转载 作者:行者123 更新时间:2023-11-29 02:52:19 39 4
gpt4 key购买 nike

我正在使用一个在自定义单元格中有一个 textFiled 的 UITableView。

在事件 textField EditingDidEnd 上:

@IBAction func saveValue(sender: UITextField) {

var cell = sender.superview.superview
var cellIndexPath = self.tableView.indexPathForCell(cell)

}

在字符串“var cellIndexPath = self.tableView.indexPathForCell”上我得到了错误

"could not find member indexPathForCell"

如何在 swift 中使用 indexPathForCell?

最佳答案

那是因为编译器已将 cell 推断为 UIView 类型,并且 indexPathForCell() 需要一个 UITableViewCell .您可以将其显式转换为 UITableViewCell,如下所示:

var cell = sender.superview.superview as UITableViewCell

但是,这不是从其 subview 之一(在您的情况下为 UITextField)获取单元格或其 indexPath 的可靠方法。

你应该去:

let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView)
let cellIndexPath = self.tableView.indexPathForRowAtPoint(pointInTable)

关于ios - 找不到成员 indexPathForCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24354135/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com