[英]Custom textfield inside a cell text is blank when Ui Tableview Scrolled
[英]How do I set the text of a custom cell's textfield to a variable inside a custom object array in Swift?
我有一个带有自定义单元格的表格视图,每个单元格都有一个文本字段。 我想将文本放在文本字段中,并将其放在每个单元格的[classes]:[ClassObject] .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
}
这是自定义类对象
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()
}
}
最后是自定义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
}
}
EditCell
是否正确设置? 即它可以显示例如调试字符串:
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 }
您的模型是否正确设置? 即您可以记录其内容:
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 }
假设您有一个EditCell
如下所示:
class EditCell : UITableViewCell {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var titleTF:UILabel
}
和aClass
结构aClass
定义为:
struct aClass {
var title:String
}
最后是这样定义的aClass
数组:
var classes:[aClass] = [aClass.init(title: "demo")]
那么下面的代码可能无法编译,除非您注释掉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
}
导致这种分析; 如果您实际上是想获取单元格的内容并将其放回数组中,则此句子似乎暗示:
我想将文本放在文本字段中,并将其放在每个单元格的[classes]:[ClassObject] .title中。
这样做的地方不在dequeueReusableCell
,而恰恰相反。 更改模型的唯一时间是更改单元时,因为单元是瞬态的并且可以重复使用,而不保留任何数据。
从评论
用户界面单元是临时的UI元素。 它们也被重用。
换句话说,只有可见单元存在(如果有的话),并被回收以显示数据。
您无法循环浏览以返回数据。 您必须立即将用户更改存储到模型(或其副本)中,并且肯定不要依赖于视图来为您存储用户数据。 这是MVC设计的基础:模型就是数据。
保存的动作等同于提交新模型。
提示: 如果使用NSFetchedResultsController
,那么很多样板工作将由OS完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.