2014-01-11 35 views
3

我將UITableView放到UIView上,其中包含我的UIScrollView。我已將代理和數據源設置爲我的UIViewController,但我注意到tableView:didSelectRowAtIndexPath方法未被調用。我沒有touchesBegan或任何其他觸摸*方法重寫。我在這裏嘗試了第4個答案:UIScrollView touchesBegan繼承我的UIScrollView,但該方法仍然未被調用。表格單元格已正確填充。爲什麼tableView:didSelectRowAtIndexPath被包含在UIScrollView中的UITableView被調用?

+0

發表一些關於如何添加tableview的代碼? – johnMa

+0

檢查uitableView的用戶交互。它應該被啓用? –

回答

4

不要在UIScrollView上實現任何UITapGestureRecognizer。或touchesBegin在UIScrollView類。它會工作。

+0

啊,沒有意識到輕拍手勢識別器將是罪魁禍首。 Sooooo,因爲我實際上需要這個水龍頭手勢識別器,有沒有辦法讓他們都能工作? – Architekt

+1

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch :(UITouch *)touch { return![NSStringFromClass([touch.view class])isEqualToString:@「UITableViewCellContentView」]; } – Xeieshan

+0

我完全看着那個想法。感謝您指出了這一點。 – GoGreen

2

我是否正確理解UITableViewUIView的子視圖,並且UIViewUIScrollView的子視圖?

爲什麼它的價值,這在我的iOS 7模擬器(我收到-tableView:didSelectRowAtIndexPath:消息)罰款。這裏有一個視圖控制器,可以用來測試我的實現。

@interface ContainedScrollersViewController() <UITableViewDataSource, UITableViewDelegate> 
@property (strong, nonatomic) UIScrollView *scrollView; 
@property (strong, nonatomic) UIView *tableViewContainer; 
@property (strong, nonatomic) UITableView *tableView; 
@property (strong, nonatomic) NSMutableArray *values; 
@end 

@implementation ContainedScrollersViewController 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     int countValues = 20; 
     _values = [[NSMutableArray alloc] initWithCapacity:countValues]; 
     for (int i = 0; i < countValues; i++) { 
      [_values addObject:@(arc4random() % 100)]; 
     } 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _scrollView = [[UIScrollView alloc] init]; 
    _scrollView.backgroundColor = [UIColor grayColor]; 
    _tableViewContainer = [[UIView alloc] init]; 
    _tableViewContainer.backgroundColor = [UIColor lightGrayColor]; 
    _tableView = [[UITableView alloc] init]; 
    _tableView.delegate = self; 
    _tableView.dataSource = self; 

    [self.view addSubview:_scrollView]; 
    [_scrollView addSubview:_tableViewContainer]; 
    [_tableViewContainer addSubview:_tableView]; 
} 

- (void)viewDidLayoutSubviews 
{ 
    self.scrollView.frame = self.view.bounds; 
    // arbitrary sizes to visualize each view in the hierarchy 
    self.scrollView.contentSize = CGSizeMake(640.0f, 960.0f); 
    self.tableViewContainer.frame = CGRectMake(0.0f, 0.0f, 400.0f, 600.0f); 
    self.tableView.frame = self.view.bounds; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.values.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:@"%@", [self.values objectAtIndex:indexPath.row]]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"selected row:%i", indexPath.row); 
} 

@end 
相關問題