UIButton Uilabel在UITableViewCell内部点击时重置

时间:2014-07-16 21:08:04

标签: ios uibutton storyboard

我有一个自定义的tableview单元格,当我单击单元格内的“数量按钮”时,单元格本身的值将重置为故事板上定义的原型单元格的值(即“1”)。所以想象一下,我点击第一个单元格图像下的按钮,按钮的标签从“5”变为“1”。当按下按钮时,我注释掉了所有代码,因此假设没有任何形状在那里变形。这是为什么?

enter image description here

我的自定义单元格(.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

我的自定义单元格(.m)

@implementation BLCartItemCell

@end

在我的视图控制器中:

- (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;

我删除了与按钮相关的IBActions,问题仍然存在。

1 个答案:

答案 0 :(得分:4)

我认为在设置buttonTitle时会出现问题

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

尝试:

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

了解更多

Text change on UIButton doesn't stick

Apple Document