[英]Objective C - Swipe to delete triggers button in tableview cell
我有一个表格视图,用户可以在其中使用按钮点击单元格来编辑单元格内容。 当我使用滑动删除功能时,“编辑按钮”会被触发并导致应用崩溃。 当我“刷卡删除”时,如何确保禁用单元格中的其他按钮?
编辑:添加示例代码。
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”功能。
谢谢。
听起来您是在代码中创建按钮并将其直接添加为单元格的子视图。 这将导致您看到的行为。
当对UITableViewCell
或UICollectionView
单元进行子类化时,不应将视图直接添加为self
子视图。 相反,您必须将它们添加为self.contentView
子视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.