简体   繁体   中英

Objective C - Swipe to delete triggers button in tableview cell

I have a tableview where the user can edit the cell content by tapping the cell using a button. When I use the swipe to delete function the 'edit button' gets triggered and causes the app to crash. How can I make shure other buttons in the cell are disabled when I 'swipe to delete'?

EDIT: example code added.

CustomCell.h:

@interface CustumCell : UITableViewCell {
   UILabel *cellText;
   UIButton *editButton;
 }

 @property (nonatomic,retain) UILabel *cellText;
 @property (nonatomic,retain) UIButton *editButton;

 @end

CustomCell.m:

@implementation CustumCell
@synthesize cellText,editButton;

- (void)awakeFromNib {

    self.backgroundColor = [UIColor clearColor];
    cellText = [[UILabel alloc] init];
    cellText.translatesAutoresizingMaskIntoConstraints=NO;
    [self.contentView addSubview:cellText];

    //Cell Constraints
    NSArray *verticalConstraintsCell =
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-6-[cellText(>=31)]"
                                        options: 0
                                        metrics:nil
                                          views:NSDictionaryOfVariableBindings(cellText)];


    NSArray *horizontalConstraintsCell =
   [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[cellText(>=2)]-50-|"
                                        options: 0
                                        metrics:nil
                                          views:NSDictionaryOfVariableBindings(cellText)];

     [self.contentView addConstraints:verticalConstraintsCell];
     [self.contentView addConstraints:horizontalConstraintsCell];

   editButton = [[UIButton alloc] init];
   [self.contentView addSubview:editButton];

}

TableViewController:

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

     CustumCell *cell= (CustumCell *)[tableView dequeueReusableCellWithIdentifier:@"CellSection1" forIndexPath:indexPath];

     if (cell==nil) {cell = [[CustumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellSection1"];}

     // add cell content.. 

    //frame editButton:
    cell.editButton.frame= CGRectMake(tableView.frame.size.width/2, 0, tableView.frame.size.width/2-50, 43);
    [cell.editButton addTarget:self action:@selector(editButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

return cell;

}

Now when I swipe to delete, the 'editButtonPressed' function executes.

Thanks.

It sounds like you are creating your button in code and adding it directly as a subview of the cell. This will cause the behavior you're seeing.

When subclassing a UITableViewCell or UICollectionView cell you should not add views directly as subviews of self . Instead, you must add them as subviews of self.contentView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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