2013-04-12 47 views
9

搶斷觸摸我有類似this one問題,但答案只要沒有太大的幫助。 我UITableView一些定製UITableViewCells,這些細胞有一定的嵌套自定義UIViews最後一些UIButtons內。正如上面指出的那樣,問題在於當我觸摸我的按鈕時,觸摸事件不會填充到UITableView,並且它從不滾動。的UIButton內的UITableViewCell從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.不要關注我提供的代碼的愚蠢,我意識到所有關於它的問題。我只是提供它來顯示問題的全部內容。這是buttonViewscrollView的衝突。

回答

9

這是UIScrollView其中tableviews使用標準的行爲。系統不知道要滾動,直到你移動你的手指,但那時的按鈕,你已經「壓」,所以它假定這就是你想要做什麼。

您可以與您的tableview的滾動視圖去改變幾個屬性的發揮,但你會發現,他們的感覺產生負面影響您的細胞,因爲加入延遲的按鈕。

self.tableView.delaysContentTouches = YES; 

delaysContentTouches 如果此屬性的值是YES,滾動視圖延遲處理該下接觸手勢,直到它可以決定是否滾動是意圖...

self.tableView.canCancelContentTouches = YES 

個canCancelContentTouches 如果該屬性的值是YES,並在內容的圖已經開始跟蹤手指觸摸它,如果用戶拖動手指夠以發起滾動,視圖接收touchesCancelled:withEvent:方法消息和滾動視圖將觸摸作爲滾動來處理。

+8

我仍然無法得到這個'UITontrol'在'UITableView'裏面工作。此外,因爲'UITableView'是'UIScrollView'的子類,所以你的代碼應該是'self.tableView.canCancelContentTouches = YES;' – johnboiles

+1

@johnboiles你應該設置elf.tableView.scrollView.delaysContentTouches = NO; – passol

+0

答案中顯示的組合不起作用。 @johnboiles是正確的。剛剛爲我工作的是:mytable.delaysContentTouches = false和mytable.canCancelContentTouches = true。這使得我的細胞內的按鈕就像平常一樣工作。 – zumzum

0

下面的代碼工作對我來說:

- (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 
} 
+0

那麼,它不適合我 - 觸摸按鈕仍然可以防止滾動tableView。不管怎麼說,還是要謝謝你。 – DemoniacDeath

7

您可以通過在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 
+0

感謝yooooouuuuu!現在表格視圖頁腳中的單元格和按鈕的行爲完全相同......;) – Trenskow

+0

完美的答案,像魅力一樣工作。 –

+0

可以確認此作品。只要確保將它與'tableView.delaysContentTouches = false'耦合即可。 – mxcl

1

因爲以上所有答案都不適用於我。我在按鈕上添加一個imageView,並使imageView用戶界面爲YES,然後在iamgeView上添加一個輕擊手勢。在輕拍手勢相關的方法中,我將按鈕相關的方法放在中。所以一切都很好.. 可能是破解。但它工作得很好..

相關問題