Issue with row selection UITableView inside UIScro

2019-07-31 02:40发布

问题:

I have a view controller with following layout:

  • Container View
    • UIScrollView
      • UITableView as a sub view of a UIScrollView

I have another UITableViewController in which I have a few rows and some methods when the row gets selected. Now I want to display this UITableview inside the UIScrollView. So I add the UITableView as a subview of UIScrollView. The table is displayed in the scroll view just fine, but when I tap in the scroll view to select the table's row, then row is being highlighted but the method is not getting called when the row is selected..

    PBDashboardPickupTable *dashtable = [[PBDashboardPickupTable alloc]initWithNibName:@"PBDashboardPickupTable" bundle:nil];
    [self.scrollView addSubview:dashtable.tableView];

Also I have set scroll view's delayContentTouches to YES and cancelContentTouch to NO from Interface Builder. Also userInteractionEnabled is set to YES... then why is the method not getting called when I tap the table view's row?

回答1:

Apple specifically warns in the documentation to avoid putting a UITableView inside of a UIScrollView:

Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

Since UITableView inherits from UIScrollView, I would suggest you add any additional views you need as a tableHeaderView, tableFooterView, or as custom cells in the table.