UIButton Label Resets When Clicked Inside A TableViewCell
ios, storyboard, uibutton
Solution
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
Problem
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? 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.