0

如何在自定义单元格类内的函数中找出活动单元格的 indexpath.row?

我用这个:

protocol ItemTableViewCellDelegate: NSObjectProtocol {
    func textFieldDidEndEditing(text: String, cell: ItemTableViewCell)
}

class ItemTableViewCell: UITableViewCell, UITextFieldDelegate {

    @IBOutlet weak var itemTitle: UITextField!
    @IBOutlet weak var date: UILabel!

    var delegate: ItemTableViewCellDelegate?


    override func awakeFromNib() {
        itemTitle.delegate = self
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        if let text = textField.text {
            delegate?.textFieldDidEndEditing(text: text, cell: self)

        }
        //BTW: everything below this comment runs every time I want it to, so no problem about that
        items.insert(itemTitle.text!, at: //(Here I want the indexpath))
    }


}

所以,我想随着文本字段的变化更新我的数组。为此,我需要找出索引 path.row。我试着把它放进去: items.insert(itemTitle.text!, at: indexPath.row)

但它不允许我这样做。

如果这在单元类中不可能做到,我也愿意考虑如何在主类中完成。

我的观点截图:

在此处输入图像描述

4

1 回答 1

1

在 ItemTableViewCell 类中添加 IndexPath 变量

var indexPathForCell: IndexPath?

在你的父视图控制器类 cellForRowAtIndexPath:-

let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier", for: indexPath) as! ItemTableViewCell

cell.indexPathForCell = indexPath

return cell
于 2017-02-24T05:57:40.483 回答