【发布时间】:2016-04-07 19:10:35
【问题描述】:
我在 UITableView 内滚动时遇到问题。 对于这个表,我创建了一个带有 2 个标签和 2 个按钮的自定义 UITableViewCell。 第一个标签是产品名称,第二个标签是数量。这 2 个按钮是 +(加号)和 -(减号)按钮。
情况
该应用程序是一个订单系统。当客户想要产品时,他/她点击加号按钮。减号按钮 出现并且数量标签随着 1 增长(所以从 0 到 1,或从 1 到 2,等等)。
问题
当用户滚动表格时,其他单元格会变为通过加号按钮“选中”的单元格。 因此,用户看到有数量的产品,而他/她根本不想要它。
--
如何防止 TableView '刷新' TableCells?
cellForRowAtIndexPath 方法:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ProductenCell", forIndexPath: indexPath) as! ProductenCell
let object = productArray[indexPath.row]
cell.label.text = (object.valueForKey("productNaam") as! String)
// Plus-button
cell.plusKnop.addTarget(self, action: "productToevoegen:", forControlEvents: .TouchUpInside)
cell.plusKnop.tag = indexPath.row
// Minus-button
cell.minKnop.addTarget(self, action: "productVerwijderen:", forControlEvents: .TouchUpInside)
cell.minKnop.tag = indexPath.row
// Grey backgorund fo even cells
if ((indexPath.row % 2) == 1) {
cell.backgroundColor = UIColor(red: 0.961, green: 0.961, blue: 0.961, alpha: 1)
}
return cell
}
感谢您的帮助:-)
【问题讨论】:
-
我将不得不查看在单击按钮时调用的加号和减号函数,但我的猜测是您将值存储在视图中,但是当您滚动视图时,另一个产品的模型会重用该视图.所有值都应存储在模型中,以便更新视图不会使您的模型无效。
标签: ios swift uitableview