UITableViewCell - 向上或向下滚动时内容重置
UITableViewCell - content reset when scrolling up or down
我遇到这个问题,我的原型动态 UITableViewCell 上的数据值在向上或向下滚动时被重置。
我正在按照其他线程中的建议检查 cell 是否为 nil,因为 cell 被重用但执行永远不会遇到 if.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Currency formatter
let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
formatter.locale = NSLocale(localeIdentifier: "el-GR")
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "MenuItemTableViewCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MenuItemTableViewCell
if cell.isEqual(nil) {
cell = UITableViewCell(style: .Default, reuseIdentifier: "MenuItemTableViewCell") as! MenuItemTableViewCell
}
// Match category (section) with items from data source
let itemsPerSection = items.filter({ [=10=].category == self.categories[indexPath.section] })
let item = itemsPerSection[indexPath.row]
// cell data
cell.cellTitle.text = item.name + " " + formatter.stringFromNumber(item.price)!
cell.cellDescription.text = item.description
有什么帮助吗?
此致,
城邦
每次单元格离开屏幕并再次打开时,都会重新创建单元格。因此,如果您修改单元格(即,如您使用步进器描述的那样),则必须保存修改后的值,以便下次出现在屏幕上时使用该修改后的值创建它。
我遇到这个问题,我的原型动态 UITableViewCell 上的数据值在向上或向下滚动时被重置。
我正在按照其他线程中的建议检查 cell 是否为 nil,因为 cell 被重用但执行永远不会遇到 if.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Currency formatter
let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
formatter.locale = NSLocale(localeIdentifier: "el-GR")
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "MenuItemTableViewCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MenuItemTableViewCell
if cell.isEqual(nil) {
cell = UITableViewCell(style: .Default, reuseIdentifier: "MenuItemTableViewCell") as! MenuItemTableViewCell
}
// Match category (section) with items from data source
let itemsPerSection = items.filter({ [=10=].category == self.categories[indexPath.section] })
let item = itemsPerSection[indexPath.row]
// cell data
cell.cellTitle.text = item.name + " " + formatter.stringFromNumber(item.price)!
cell.cellDescription.text = item.description
有什么帮助吗?
此致, 城邦
每次单元格离开屏幕并再次打开时,都会重新创建单元格。因此,如果您修改单元格(即,如您使用步进器描述的那样),则必须保存修改后的值,以便下次出现在屏幕上时使用该修改后的值创建它。