[英]UIButton inside UITableViewCell slides in from side when animating UITableView
[英]UIButton inside UITableViewCell steals touch from UITableView
我有一個類似於這個的問題,但答案提供沒有多大幫助。 我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
唯一單元格中的紅色按鈕不會讓表格視圖反彈滾動。
任何人都可以幫助我一點嗎?
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
這是表視圖使用的UIScrollView
的標准行為。 在您移動手指之前,系統不知道您要滾動,但到那時您已經“按下”了按鈕,因此它假定您想要做的事情。
您可以在tableview的scrollview上使用幾個屬性來更改行為,但是您可能會發現它們會因為延遲而增加對單元格和按鈕的感覺產生負面影響。
self.tableView.delaysContentTouches = YES;
delaysContentTouches如果此屬性的值為YES,則滾動視圖會延遲處理觸摸手勢,直到它可以確定滾動是否為意圖...
和
self.tableView.canCancelContentTouches = YES
canCancelContentTouches如果此屬性的值為YES並且內容中的視圖已開始跟蹤手指觸摸它,並且如果用戶拖動手指足以啟動滾動,則視圖將收到touchesCancelled:withEvent:消息並且滾動視圖句柄作為滾動的觸摸。
因為以上所有答案都不適合我。 我在按鈕上添加了一個imageView並使imageView用戶界面為YES,然后在iamgeView上添加一個輕擊手勢。 在點擊手勢相關的方法中我把按鈕相關的方法放入。所以一切都很好..可能是黑客。 但它運作良好..
下面的代碼適用於我:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id"];
cell.userInteractionEnabled = YES;
if (cell == nil) {
cell = [[[CustomCell1 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell_id"]autorelease];
}
[cell.button addTarget:self action:@selector(button_action) forControlEvents:UIControlEventTouchUpInside];}
-(void)button_action{NSLog(@"Hello!");}
這是我的自定義單元格:
#import "CustomCell.h"
@implementation CustomCell
@synthesize button;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
button = [[UIButton alloc]init];
button =[UIColor redColor];
[self.contentView addSubview: button];
}
return self;}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+50 ,+15, 100, 100);
button = frame;}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{ [super setSelected:selected animated:animated];
// Configure the view for the selected state
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.