5

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?

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
kidsid49
  • 1,358
  • 3
  • 18
  • 37
  • why don't you use Sections of the TableView . Using single tableView. – Fatti Khan Nov 09 '15 at 07:54
  • could you please attach image of that for better idea. – Viraj Padsala Nov 09 '15 at 08:08
  • Why are you using multiple tableViews? just use the data model delegate methods to change the tableView presented data. – YYfim Nov 09 '15 at 08:58
  • Yeah but when I will reload tableview it will refresh the section header view as well which has that segment control and I have to keep remember the scroll offset of when user jumps from one selection to another. – kidsid49 Nov 14 '15 at 16:15
  • @VirajPadsala think its like twitter only. Different tabs for "tweets,Media and likes" for user profile. – kidsid49 Nov 14 '15 at 16:16
  • 1
    Did you see this question? http://stackoverflow.com/questions/31212448/twitter-ios-profile-page-configuration – Tim Nov 17 '15 at 03:17
  • childview controllers with custom uiviews told multiple child of children. it's all very complicated but this method works and it responds to all touch events correctly. I won[t post code becasue it's too hard for me to explain but the point is you create a scrollview in the main VC add three views inside this scroll view, add children to each view, then when you call each child create a weakslef to strong self pointer to the VCs in question. store the VCs in an NSMutable dictionary, and information will persiste with views. good luck – Larry Pickles Nov 19 '15 at 00:56
  • Use this two line code that make your child table scroll first and if it reaches to end or top then your main scroller will be scroll. `self.scroller.delaysContentTouches=YES; self.scroller.canCancelContentTouches=NO;` – Viraj Padsala Nov 21 '15 at 06:31

4 Answers4

3

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.

2
  • 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


enter image description here


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

Dhaivat Vyas
  • 2,926
  • 1
  • 16
  • 26
  • Query: while your solution seems to work, doesn't it breaks the concept of `UIViewControllers`, objects that in essence manage a single view to be pushed or presented via navigation or tab bar? – SwiftArchitect Nov 19 '15 at 23:52
  • No it will not break concept of `UIViewControllers` as you are using `UIContainerView` and you will require separate classes for each view controller which will split your code from single view controller and help in managing the code. you can visit : [UIContainerView](https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html) – Dhaivat Vyas Nov 20 '15 at 04:19
1

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) {
}];

Chinh Ngo
  • 33
  • 1
  • 3
1

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 ...

TheEye
  • 9,280
  • 2
  • 42
  • 58