【发布时间】:2016-01-21 16:05:23
【问题描述】:
我有一个UITableView 填充了带有按钮的单元格。我希望这些按钮在编辑模式下也能工作,但它们没有。 UITableViewCell 上的手势识别器似乎正在阻止按钮上的手势识别器。有人对解决这个问题有什么建议吗?
ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
self.items = [@[@"one", @"two", @"three", @"four"] mutableCopy];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cellID"
forIndexPath:indexPath];
if (cell == nil) {
cell = [[TableViewCell alloc] init];
}
[cell.button setTitle: self.items[indexPath.row]
forState: UIControlStateNormal];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.items removeObjectAtIndex: indexPath.row];
}
}
TableViewCell.h:
@interface TableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *button;
- (IBAction)buttonTapped:(id)sender;
@end
所以,buttonTapped: 在单元格不处于编辑模式时被调用,但当单元格处于编辑模式时,按钮不起作用。
【问题讨论】:
-
您可能希望显示您使用的代码,1- 如何添加按钮,2- 如何启用“编辑”模式,3- 您使用的任何手势识别器。
-
我指的是 UIKit 对手势识别器的使用。故事板中添加了按钮
标签: ios objective-c uitableview cocoa-touch uikit