Swift将变量传递给UItableCell

时间:2017-07-20 17:50:56

标签: ios swift xcode

我试图将变量传递给UITableViewCell。目前即时使用mycell.myvar = "mystring"这是行不通的还有其他方法可以使这项工作。下面的例子应该打印"你好"但它不起作用

cellForRow

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: self.reuseCell, for: indexPath) as! CustomFormCellTextField
        cell.myString = "hello"
        return cell
    }

了myCell

class CustomFormCellTextField:UITableViewCell {
    var customTextField:JVFloatLabeledTextField = JVFloatLabeledTextField()
    var myString:String = String()
     override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        print(myString)
            self.addSubview(customTextField)
            constrain(self,customTextField) { (cell,row) in
                row.left == cell.left
                row.right == cell.right
                row.top == cell.top
                row.bottom == cell.bottom
            }
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

1 个答案:

答案 0 :(得分:0)

在CustomFormCellTextField中输入:

var myString: String? {
    didSet {
        guard let text = myString else { return }
        print(text)
    }
}