3

I have a custom tableview cell, when I click the "quantity button" inside the cell, the value of the cell itself resets to the value defined the prototype cell on storyboard (which is "1"). So imagine I click the button under the image of the first cell, the label of the button changes from "5" to "1". I commented out all the code when button is pressed, so assume nothing ishapening there. Why is this?

enter image description here

My Custom Cell (.h)

@interface BLCartItemCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UIButton *quantityBtn;
@property (weak, nonatomic) IBOutlet UIButton *removeBtn;
@property (weak, nonatomic) IBOutlet UILabel *price;

@end

My Custom Cell (.m)

@implementation BLCartItemCell

@end

In my view controller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    // Section 0: Cart cells
        static NSString *cartCellIdentifier = @"cartCell";

        BLCartItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cartCellIdentifier];

        if (cell == nil) {
            cell = [[BLCartItemCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cartCell"];
//            [cell.removeBtn addTarget:self action:@selector(removeBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
//            [cell.quantityBtn addTarget:self action:@selector(quantityBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        }

        // Grab the cart item from cart and set the cell properties
        BLCartItem *cartItem                    = [self.cart.items objectAtIndex:indexPath.row];
        cell.title.text                         = cartItem.title;
        cell.quantityBtn.titleLabel.text        = [NSString stringWithFormat:@"%i", cartItem.quantity];
        cell.price.text                         = [NSString stringWithFormat:@"$ %.2f", [cartItem totalPrice]];
        NSData *image_data                      = [[NSData alloc] initWithContentsOfURL:cartItem.image];
        cell.imgView.image                      = [UIImage imageWithData:image_data];

        // Buttons will keep track of cell index
        cell.quantityBtn.tag                    = indexPath.row;
        cell.removeBtn.tag                      = indexPath.row;

        return cell;

I removed the IBActions associated with the button, problem still happens.

blee908
  • 12,165
  • 10
  • 34
  • 41
  • Did you set initial value of the int number to 1? I think it's being called twice, after you made changes. You need to save temporary int to NSUserdefaults or JSON file and load from there and not set to default each time to click it. – Frans Raharja Kurniawan Jul 16 '14 at 21:12
  • Can you show us the code behind this? My guess is that your model is somehow resetting the value to "1" every time, not that it's being reset to the prototype value. – NRitH Jul 16 '14 at 21:22
  • provide the table view delegate code where the cells are being created and the button press – hackerinheels Jul 16 '14 at 21:33
  • There you go. I changed my the UIButton default value to 0 in storyboard. When clicked, it goes to 0. – blee908 Jul 16 '14 at 21:47
  • Also, when I uncommented my "addToTarget" function, I console logged the button's title and it's "1"! That means before the touchupinside event gets sent, the button has somehow changed already. – blee908 Jul 16 '14 at 22:07
  • Did you check the value stored in cartItem.quantity? – Ritu Jul 17 '14 at 07:22

1 Answers1

4

I think it has problem when you set buttonTitle like that

cell.quantityBtn.titleLabel.text = [NSString stringWithFormat:@"%i", cartItem.quantity];

try :

[cell.quantityBtn setTitle: [NSString stringWithFormat:@"%i", cartItem.quantity] forState: UIControlStateNormal]];

Read more

Text change on UIButton doesn't stick

Apple Document

Community
  • 1
  • 1
Huy Nghia
  • 996
  • 8
  • 22