【问题标题】:UITableViewCell with Save button. textField and a label带有保存按钮的 UITableViewCell。文本字段和标签
【发布时间】:2016-12-08 23:07:18
【问题描述】:

我想快速创建一个带有 2 个原型单元格的 tableView,在一个单元格中我有一个标签和一个文本字段,在另一个单元格中我有一个保存按钮,@IBOutlet 和 @IBAction 在一个单独的 UITableViewCell 文件中,我想在文本字段中写一些东西,当单击保存按钮时,必须用 textField.text 更改 label.text,但是我有一个问题,当我单击按钮时,textField 的值为 nil。我现在很快,如何解决这个问题? 谢谢

【问题讨论】:

  • 你为什么要在 2 个不同的单元格中这样做?只有2个细胞?显示一些代码

标签: swift uitableview


【解决方案1】:

我已经解决了标签的问题

cell.propertyName.tag = indexPath.row

还有这个代表

    func textField(textField: UITextField, shouldChangeCharactersInRange range:NSRange, replacementString string: String) -> Bool {
    if textField.tag == 0 {
        CoreDataController.sharedIstanceCData.matsDataField = "" + textField.text!+string
    } else {
        CoreDataController.sharedIstanceCData.commentDataField = "" + textField.text!+string
    }
    return true
}

它解决了我的问题,tnx all

【讨论】:

    【解决方案2】:

    图片中的场景是这样的

    有两个文件,TableViewController.swift 里面有这个

        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCellWithIdentifier("cella", forIndexPath: indexPath) as! TableViewCell
            return cell
        }
        let cell = tableView.dequeueReusableCellWithIdentifier("saveCell", forIndexPath: indexPath) as! TableViewCell
        return cell
    }
    

    第二个文件名为 TableViewCell.swift

    import UIKit
    

    类 TableViewCell: UITableViewCell {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var label: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    
        // Configure the view for the selected state
    }
    
    @IBAction func saveBtn(sender: AnyObject) {
        self.label.text = textField.text
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-22
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      相关资源
      最近更新 更多