简体   繁体   English

自定义UITableViewCell在滚动TableView时发生更改

[英]Custom UITableViewCell changes when TableView scrolled

I have a problem with scrolling within the UITableView. 我在UITableView中滚动时遇到问题。 For this table, I created a custom UITableViewCell with 2 labels and 2 buttons. 对于此表,我创建了一个带有2个标签和2个按钮的自定义UITableViewCell。 The first label is a productname, the second label is the amount. 第一个标签是产品名称,第二个标签是金额。 The 2 buttons are + (plus) and - (minus) buttons. 2个按钮是+(加号)和-(减号)按钮。

The situation 情况

The app is an order-system. 该应用程序是一个订单系统。 When a customer wants a product, he/she taps the plus button. 当客户想要产品时,他/她点击加号按钮。 The minus-button appears and the amount label grows with 1 (so from 0 to 1, or 1 to 2, etc.). 出现减号按钮,金额标签以1递增(因此从0到1,或从1到2,依此类推)。

The problem 问题

When a user scrolls through the table, the other cells changes to the cells who are 'selected' via the plus-button. 当用户在表中滚动时,其他单元格将变为通过加号按钮“选中”的单元格。 Hereby the user sees products that having an amount while he/she doens't want it at all. 因此,用户看到的产品具有一定数量,而他/她根本不需要。

-- -

What can I do to prevent the TableView from 'refreshing' the TableCells? 如何防止TableView“刷新” TableCell?

The cellForRowAtIndexPath-method: 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  
} 

Thanks for the help :-) 谢谢您的帮助 :-)

You should use the model objects to store amount and update every cell according to model. 您应该使用模型对象存储数量并根据模型更新每个单元格。 It is wrong to store data in views. 在视图中存储数据是错误的。

您需要将要更改的值保存到productArray或其他地方,然后在cellForRowAtIndexPath中将此值还原到单元格中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM