gpt4 book ai didi

ios - UITableView 更改一个单元格中的文本,其他单元格中的文本也更改

转载 作者:行者123 更新时间:2023-11-28 13:12:02 27 4
gpt4 key购买 nike

我有一个带有自定义 UITableViewCell 的 UITableView 设置,它有一个标签和一个文本字段。每当我现在更改文本字段的文本时,其他随机单元格的文本字段中的文本也会更改。

public class ChangePriceTableViewCell: UITableViewCell {

@IBOutlet weak var priceField: UITextField!
@IBOutlet weak var productName: UILabel!
@IBAction func changePrice(sender: UITextField) {
println(sender.text)
}

public override func layoutSubviews(){

var toolbar = UIToolbar.new()
toolbar.frame.size.height = 35

var doneButton:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "hideKeyboard")

var space:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)

var items = [AnyObject]()
items.append(space)
items.append(doneButton)

toolbar.items = items

priceField.inputAccessoryView = toolbar
}

public func configure(product: String) {
self.productName.text = product


}

func hideKeyboard() {
println("Test")
priceField.resignFirstResponder()
}


}

和数据源:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 12//self.products.keys.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//println(self.products[self.products.keys[section]])
return 2//self.products[self.products.keys[section]]!.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("change") as! ChangePriceTableViewCell

//var prods = self.products[self.products.keys[indexPath.section]]
//var productName = prods?[indexPath.row] as! String
//cell.configure(productName)

cell.configure("Test")
return cell
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Test"//self.products.keys[section].uppercaseString
}

改变的单元格总是在屏幕外。有时,这些值甚至会在两个单元格之间跳跃!

最佳答案

您是重复使用的细胞。请在 ChangePriceTableViewCell 中实现此方法:

func prepareForReuse() // if the cell is reusable (has a reuse identifier), this is called just before the cell is returned from the table view method dequeueReusableCellWithIdentifier:.  If you override, you MUST call super.

并为单元格设置正确的默认值。

关于ios - UITableView 更改一个单元格中的文本,其他单元格中的文本也更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964685/

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