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