gpt4 book ai didi

ios - 为什么插入后出现删除的行而不是新行?

转载 作者:行者123 更新时间:2023-11-28 12:38:23 34 4
gpt4 key购买 nike

我有一个带有文本字段的自定义单元格。我以标准方式进行行删除:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
dataArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cellId", forIndexPath: indexPath) as! MyCell

myCell.textField.placeholder = dataArray[indexPath.row]

return myCell
}

我还为单元格使用了我自己的类。

class MyCell: UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}

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

let textField: UITextField = {
var field = UITextField()
field.placeholder = "Placeholder Text"
field.translatesAutoresizingMaskIntoConstraints = false
return field
}()

func setupViews() {
addSubview(textField)

//Constraints stuff
}

问题是如果文本字段包含文本并且我删除了该行,则在插入新行后我会得到带有文本的旧行。

如何避免?我需要干净的行。

顺便说一句,动画也不能正常工作。

感谢您的帮助!

最佳答案

使用 UITableViewCellprepareForReuse() 方法。

func prepareForReuse() {
textField.text = nil
}

更多信息 here .

关于ios - 为什么插入后出现删除的行而不是新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40219954/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com