简体   繁体   English

目标C-在表格视图单元格中滑动以删除触发器按钮

[英]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: CustomCell.h:

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

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

 @end

CustomCell.m: 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: 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. 现在,当我滑动删除时,将执行“ editButtonPressed”功能。

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 . 当对UITableViewCellUICollectionView单元进行子类化时,不应将视图直接添加为self子视图。 Instead, you must add them as subviews of self.contentView . 相反,您必须将它们添加为self.contentView子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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