[英]Custom UITableViewCell changes when TableView scrolled
我在UITableView中滾動時遇到問題。 對於此表,我創建了一個帶有2個標簽和2個按鈕的自定義UITableViewCell。 第一個標簽是產品名稱,第二個標簽是金額。 2個按鈕是+(加號)和-(減號)按鈕。
情況
該應用程序是一個訂單系統。 當客戶想要產品時,他/她點擊加號按鈕。 出現減號按鈕,金額標簽以1遞增(因此從0到1,或從1到2,依此類推)。
問題
當用戶在表中滾動時,其他單元格將變為通過加號按鈕“選中”的單元格。 因此,用戶看到的產品具有一定數量,而他/她根本不需要。
-
如何防止TableView“刷新” TableCell?
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
}
謝謝您的幫助 :-)
您應該使用模型對象存儲數量並根據模型更新每個單元格。 在視圖中存儲數據是錯誤的。
您需要將要更改的值保存到productArray或其他地方,然后在cellForRowAtIndexPath中將此值還原到單元格中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.