Objective C - 在tableview单元格

时间:2015-10-11 22:12:52

标签: ios objective-c uitableview delete-row

我有一个tableview,用户可以通过使用按钮点击单元格来编辑单元格内容。当我使用滑动删除功能时,可以使用“编辑”按钮'被触发并导致应用程序崩溃。当我“滑动删除”时,如何禁用单元格中的其他按钮被禁用?

编辑:添加了示例代码。

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;

}

现在当我滑动删除时,' editButtonPressed'功能执行。

感谢。

1 个答案:

答案 0 :(得分:0)

听起来您在代码中创建按钮并将其直接添加为单元格的子视图。这将导致您看到的行为。

在对UITableViewCellUICollectionView单元格进行子类化时,不应将视图直接添加为self的子视图。相反,您必须将它们添加为self.contentView的子视图。

相关问题