【问题标题】:Indexpath.row inside custom cell class自定义单元类中的 Indexpath.row
【发布时间】:2017-02-23 18:56:03
【问题描述】:

如何在自定义单元格类内的函数中找出活动单元格的 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)

但它不允许我这样做。

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

我的观点截图:

【问题讨论】:

  • items 数组在哪里定义?
  • 单元格应该有一个委托,它可以通知它的文本已更改,这将更新数据模型。
  • 你也可以使用闭包代替委托。
  • 数组是全局的。(我知道,这是个坏习惯)我在主类之前定义的。
  • @dan 我该怎么做?

标签: ios swift uitableview cells indexpath


【解决方案1】:

在您的 ItemTableViewCell 类中添加一个 IndexPath 变量

var indexPathForCell: IndexPath?

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

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

cell.indexPathForCell = indexPath

return cell

【讨论】:

  • 这在大多数情况下都有效,但有时它不会为单元格设置并且它
猜你喜欢
  • 2012-06-01
  • 2013-12-29
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
相关资源
最近更新 更多