Rick de Jong
Rick de Jong

Reputation: 237

Custom UITableViewCell changes when TableView scrolled

I have a problem with scrolling within the UITableView. For this table, I created a custom UITableViewCell with 2 labels and 2 buttons. The first label is a productname, the second label is the amount. The 2 buttons are + (plus) and - (minus) buttons.

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.).

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?

The cellForRowAtIndexPath-method:

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 :-)

Upvotes: 1

Views: 1041

Answers (2)

Simon Moshenko
Simon Moshenko

Reputation: 2166

You need to save your values which is changing into your productArray or somewhere else, and in the cellForRowAtIndexPath restore this values into the cell.

Upvotes: 1

kirander
kirander

Reputation: 2256

You should use the model objects to store amount and update every cell according to model. It is wrong to store data in views.

Upvotes: 1

Related Questions