我有一个tableview,用户可以通过使用按钮点击单元格来编辑单元格内容。当我使用滑动删除功能时,"编辑按钮"被触发,导致应用程序崩溃。当我"滑动删除"时,如何使单元格中的其他按钮禁用?
编辑:添加示例代码。 CustomCell.h: CustomCell.m: @interface CustumCell : UITableViewCell {
UILabel *cellText;
UIButton *editButton;
}
@property (nonatomic,retain) UILabel *cellText;
@property (nonatomic,retain) UIButton *editButton;
@end
@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];
}
表:
- (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'函数执行。
谢谢。
这听起来像你在代码中创建按钮,并将其直接添加为单元格的子视图。这将导致您看到的行为。
当子类化UITableViewCell
或UICollectionView
单元格时,不应该直接添加视图作为self
的子视图。相反,您必须将它们添加为self.contentView
的子视图。