Dem*_*ath 9 uibutton uitableview ios
我有一个类似于这个的问题,但答案提供没有多大帮助.我有UITableView
一些习惯UITableViewCells
,那些单元格有一些嵌套的自定义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
Run Code Online (Sandbox Code Playgroud)
唯一单元格中的红色按钮不会让表格视图反弹滚动.
任何人都可以帮助我一点吗?
PS不要注意我提供的代码的愚蠢,我知道有关它的所有问题.我只是提供它来展示问题的全部内容.这是冲突buttonView
和scrollView
.
您可以通过在UITableView中覆盖touchesShouldCancelInContentView:来更改行为.为此,您需要在loadView或xib文件中使用此子类替换表视图.
@interface AutoCancelTableView: UITableView
@end
@implementation AutoCancelTableView
//
// Overriding touchesShouldCanceInContentView changes the behaviour
// of button selection in a table view to act like cell selection -
// dragging while clicking a button will cancel the click and allow
// the scrolling to occur
//
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return YES;
}
@end
Run Code Online (Sandbox Code Playgroud)
这是UIScrollView
tableviews使用的标准行为.在您移动手指之前,系统不知道您要滚动,但到那时您已经"按下"了按钮,因此它假定您想要做的事情.
您可以在tableview的滚动视图中使用几个属性来更改行为,但是您可能会发现它们会因为延迟而增加对单元格和按钮的感觉产生负面影响.
self.tableView.delaysContentTouches = YES;
Run Code Online (Sandbox Code Playgroud)
delaysContentTouches 如果此属性的值为YES,则滚动视图会延迟处理触摸手势,直到它可以确定滚动是否为意图...
和
self.tableView.canCancelContentTouches = YES
Run Code Online (Sandbox Code Playgroud)
canCancelContentTouches 如果此属性的值为YES并且内容中的视图已开始跟踪手指触摸它,并且如果用户拖动手指足以启动滚动,则视图将收到touchesCancelled:withEvent:消息并且滚动视图句柄作为滚动的触摸.
归档时间: |
|
查看次数: |
11938 次 |
最近记录: |