【问题标题】:Why isn't tableView:didSelectRowAtIndexPath being called for my UITableView contained within a UIScrollView?为什么不为包含在 UIScrollView 中的 UITableView 调用 tableView:didSelectRowAtIndexPath?
【发布时间】:2014-01-30 08:55:57
【问题描述】:

我将UITableView 放到了UIView 上,它包含在我的UIScrollView 中。我已将委托和数据源设置为我的UIViewController,但我注意到tableView:didSelectRowAtIndexPath 方法没有被调用。我没有覆盖 touchesBegan 或任何其他 touches* 方法。我在这里尝试了第四个答案:UIScrollView touchesBegan 子类化我的UIScrollView,但该方法仍然没有被调用。但表格单元格已正确填充。

【问题讨论】:

  • 发布一些关于如何添加 tableview 的代码?
  • 检查 uitableView 的用户交互。应该启用吗??

标签: ios objective-c uitableview uiscrollview


【解决方案1】:

不要在 UIScrollView 上实现任何 UITapGestureRecognizer。或 touchesBegan 在 UIScrollView 类中。它会起作用的。

【讨论】:

  • 啊,没有意识到点击手势识别器会是罪魁祸首。太好了,因为我实际上需要这个轻击手势识别器,有没有办法让它们都工作?
  • - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return ![NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]; }
  • 我完全忽略了这个想法。感谢您指出这一点。
【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多