我有一个类似于这个的问题,但是那里提供的答案并没有帮助太多。
我有一个包含一些自定义UITableViewCells
的UITableView
,这些单元格中有一些嵌套的自定义UIViews
和最后一些内部的UIButtons
。就像上面提到的问题那样,当我触摸我的按钮时,触摸事件不会传播到UITableView
且它永远无法滚动。
下面是一些代码 (这只是最快的复制方式,不是我的实际代码):
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView * tableView;
@end
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,
self.view.bounds.size.width,
self.view.bounds.size.height)
style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [[UITableViewCell alloc] init];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(0, 0, 50, 44);
[cell.contentView addSubview:button];
return cell;
}
@end
只有一个单元格中的红色按钮会阻止表视图进行弹性滚动。
有人可以帮我一下吗?
P.S. 不要注意我提供的代码的愚蠢,我知道它的所有问题。我只是提供它来展示问题所在。这是和之间的冲突。
tableView.delaysContentTouches = false
结合使用即可。 - mxcl