简体   繁体   中英

How to add multiple UITableviewController inside a scroll view?

I want to make a profile viewcontroller something similar to Twitter. I believe it contains 3 different child UITableviewController which keeps switching by segmented control. But when I scroll my tableview of child viewController it scrolls the parent scroll view as well.

Can anybody explain how to achieve that behavior?

I know your problem exactly. Problem is, Twitter is not an example, Snapchat however is....

You have a root View Controller that embeds a UIScrollView (Subclassed). Inside this subclass, you want to override the gesture recognizers like so...

 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (gestureRecognizer.state != 0 && otherGestureRecognizer.state != 1)
{
    return YES;
} else {
    return NO;
}

}

Now you should be able to swipe tableViewCells and not drag in all 4 directions at once.

  • What you can do is make UIScrollView sub-view of UIViewController and then make UIContainerView sub-view of UIScrollView.
  • But by using UIContainerView will help you in splitting of code and also use multiple UITableViewController .
  • Now add UITableViewController and embed it with UIContainerView.

Please look at the images below for solution. Also check the structure in Document outline.

Using UIContainerView and UITableViewController


在此输入图像描述


Hope it helps in solving your problem. Any queries please let me know.

I also have looked at that answer for about three months ago. I used to tried to implement it with TableView paging but not success. The behavior of swipe between those child view not the same, it looks weird. Finally, I end up with using Page View Controller. Beside the swipe gesture to switch between the controllers, you can implemented the some buttons or segmented control and call this function to navigate:

[self.pageViewController setViewControllers:viewControllers direction:direction animated:YES completion:^(BOOL finished) {
}];

I'm pretty sure it's just one table (or collection view) that just switches the cell types/cell data when you push one of the three buttons - why would you want to make it more complicated?

You can also only refresh all cells beyond your top cells, and remembering the scroll offset is a piece of cake compared to other solutions ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM