简体   繁体   English

如何在Swift中将自定义单元格的文本字段的文本设置为自定义对象数组内的变量?

[英]How do I set the text of a custom cell's textfield to a variable inside a custom object array in Swift?

I have a table view with custom cells, and each cell has a text field. 我有一个带有自定义单元格的表格视图,每个单元格都有一个文本字段。 I want to take the text in the textfield and put it inside my [classes] : [ClassObject].title for each cell. 我想将文本放在文本字段中,并将其放在每个单元格的[classes]:[ClassObject] .title中。 The problem is that it won't work! 问题是它不起作用!

Here's the code 这是代码

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "newCell")! as! EditCell
    let c = classes[indexPath.row]
    cell.titleTF.text = c.title
    c.title = cell.titleTF.text

    return cell
}

And heres the custom class object 这是自定义类对象

class ClassObject: NSObject, NSCoding {

var title : String?
var detail : String?
var startTime : String?
var endTime: String?
var stuff : [String]?
var info : String?
var titleColor : UIColor?


enum state {
    case normal,red,yellow
}

func setStates(currentState : state) {
    switch currentState {
    case .normal:
        titleColor = UIColor.black
    case .red:
        titleColor = UIColor.red
    case .yellow:
        titleColor = UIColor(red: 203/255, green: 154/255, blue: 0/255, alpha: 1.0)
    }
}

init(title: String, detail: String, startTime: String, endTime: String, stuff: Array<String>, info: String, titleColor: UIColor) {
    self.title = title
    self.detail = detail
    self.startTime = startTime
    self.endTime = endTime
    self.stuff = stuff
    self.info = info
    self.titleColor = titleColor

}


required convenience init(coder aDecoder: NSCoder) {
    let title = aDecoder.decodeObject(forKey: "title")
    let detail = aDecoder.decodeObject(forKey: "detail")
    let startTime = aDecoder.decodeObject(forKey: "startTime")
    let endTime = aDecoder.decodeObject(forKey: "endTime")
    let stuff = aDecoder.decodeObject(forKey: "stuff")
    let info = aDecoder.decodeObject(forKey: "info")
    let titleColor = aDecoder.decodeObject(forKey: "titleColor")
    self.init(title: title as! String, detail: detail as! String, startTime: startTime as! String, endTime: endTime as! String, stuff: stuff as! Array<String>, info: info as! String, titleColor: titleColor as! UIColor)
}

func encode(with aCoder: NSCoder) {
    aCoder.encode(title, forKey: "title")
    aCoder.encode(detail, forKey: "detail")
    aCoder.encode(startTime, forKey: "startTime")
    aCoder.encode(endTime, forKey: "endTime")
    aCoder.encode(stuff, forKey: "stuff")
    aCoder.encode(info, forKey: "info")
    aCoder.encode(titleColor, forKey: "titleColor")
}
override init() {
    super.init()
}

} }

At last heres the custom UITableViewCell 最后是自定义UITableViewCell

class EditCell: UITableViewCell {



@IBOutlet weak var titleTF: UITextField!




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
}

} }

Prerequisites 先决条件

  1. Is EditCell properly setup? EditCell是否正确设置? ie can it display, say, a debug string: 即它可以显示例如调试字符串:

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "newCell")! as! EditCell cell.titleTF.text = "debug row \\(indexPath.row)" return cell } 
  2. Is your model properly setup? 您的模型是否正确设置? ie can you log its content: 即您可以记录其内容:

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "newCell")! as! EditCell let c = classes[indexPath.row] print("c.title at index \\(indexPath.row): \\(c.title)") cell.titleTF.text = c.title return cell } 

Discussion 讨论区

Assuming that you have an EditCell looking like this: 假设您有一个EditCell如下所示:

class EditCell : UITableViewCell {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    var titleTF:UILabel
}

and a structure aClass defined more or less like that: aClass结构aClass定义为:

struct aClass {
    var title:String
}

and lastly an array of such aClass defined like so: 最后是这样定义的aClass数组:

var classes:[aClass] = [aClass.init(title: "demo")]

then the code below cannot possibly compile unless you comment out c.title = ... 那么下面的代码可能无法编译,除非您注释掉c.title = ...

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "newCell")! as! EditCell
    let c = classes[indexPath.row]
    cell.titleTF.text = c.title
    ///c.title = cell.titleTF.text
    return cell
}

which leads to this analysis ; 导致这种分析; if you actually want to take the content of the cell and put it back into an array, as this sentence seems to imply: 如果您实际上是想获取单元格的内容并将其放回数组中,则此句子似乎暗示:

I want to take the text in the textfield and put it inside my [classes] : [ClassObject].title for each cell. 我想将文本放在文本字段中,并将其放在每个单元格的[classes]:[ClassObject] .title中。

the place to do so is not in dequeueReusableCell which is exactly the opposite. 这样做的地方不在dequeueReusableCell ,而恰恰相反。 The only time to change your model is when the cell is changed, as cells are transient and reused, not persisting any data. 更改模型的唯一时间是更改单元时,因为单元是瞬态的并且可以重复使用,而不保留任何数据。


Conclusion 结论

from comment 从评论

User Interface cells are transient, UI elements. 用户界面单元是临时的UI元素。 They also are reused. 它们也被重用。
In other words, only the visible cells exist, if at all, and are recycled to display data. 换句话说,只有可见单元存在(如果有的话),并被回收以显示数据。

You cannot cycle through the to get back to the data. 您无法循环浏览以返回数据。 You must store the user changes on the fly back into your model (or a copy of it), and positively do not rely on the view to store user data for you. 您必须立即将用户更改存储到模型(或其副本)中,并且肯定不要依赖于视图来为您存储用户数据。 This is the basis of MVC design: the model is the data. 这是MVC设计的基础:模型就是数据。

The action of saving is the equivalent of committing the new model. 保存的动作等同于提交新模型。

Hint: If you use NSFetchedResultsController , a lot of this boilerplate work is done by the OS. 提示: 如果使用NSFetchedResultsController ,那么很多样板工作将由OS完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当Ui Tableview滚动时,单元格文本内的自定义文本字段为空 - Custom textfield inside a cell text is blank when Ui Tableview Scrolled 检索自定义单元格内的textfield.text - Retrieve textfield.text which is inside custom cell 将自定义单元格标签和TextField文本追加到字典数组 - Appending Custom Cell Label and TextField text to Array of Dictionary 如何访问自定义单元格文本字段值Swift 3.0 - How to access custom cell textfield values Swift 3.0 Swift:如何将wilDisplayCell分配给特定的自定义寄存器单元? - Swift: How do I assign wilDisplayCell to specific custom register cell? 我如何在没有自定义单元格的UITableViewCell中包装文本 - How do I wrap text in a UITableViewCell without a custom cell 如何查询Parse以从自定义类获取信息并在Swift的文本字段中显示它? - How do I query Parse to get info from a custom class and display it in textfield in Swift? 如何在Swift中将UITableView的单元格文本设置为数据库单元格的内容? - How can I set a UITableView's cell text to database cell's content in Swift? 如何填充自定义单元的UIPickerView - How do I populate Custom Cell's UIPickerView 如何获得真正的自定义单元格的宽度? - How do I get the real custom cell's width?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM