Update UITableView while scrolling - objective-c

My data source for a UITableView is updated automatically each 5 seconds - this causes some issues when the users is scrolling and the data has changed (less rows etc.)
How is the best approach for handling this? How can I only perform updates when the user is not scrolling?

If you are working with a UITableView it is likely that you are already implementing the UITableViewDelegate to fill the table with data.
The protocol UITableViewDelegate conforms to UIScrollViewDelegate, so all you need to do is to implement the methods -scrollViewWillBeginDragging and -scrollViewDidScroll
So your code should look something like this:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (scrollView == self.tableView){
//your code when user is scrolling
}
else
{
//your code when user is not scrolling
}
}

UITableView is also UIScrollView. Ur UIViewController child could be set as UIScrollView delegate and handle method so u can detect if user is scrolling or not.
If u want to reload data in UITableView just call reloadData method

Related

After bouncing of table to top App get crash [duplicate]

Here's how the scroll views work: One scroll view is paging enabled in the horizontal direction. Each 'page' of this scroll view contains a vertically scrolling UITableView. Without modification, this works OK, but not perfectly.
The behaviour that's not right: When the user scrolls up and down on the table view, but then wants to flick over to the next page quickly, the horizontal flick/swipe will not work initially - it will not work until the table view is stationary (even if the swipe is very clearly horizontal).
How it should work: If the swipe is clearly horizontal, I'd like the page to change even if the table view is still scrolling/bouncing, as this is what the user will expect too.
How can I change this behaviour - what's the easiest or best way?
NOTE For various reasons, a UIPageViewController as stated in some answers will not work. How can I do this with cross directional UIScrollViews (/one is a table view, but you get the idea)? I've been banging my head against a wall for hours - if you think you can do this then I'll more than happily award a bounty.
According to my understanding of the question, it is only while the tableView is scrolling we want to change the default behaviour. All the other behaviour will be the same.
SubClass UITableView. UITableViews are subClass of UIScrollViews. On the UITableView subClass implement one UIScrollView's UIGestureRecognizer's delegate method
- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer
{
//Edit 1
//return self.isDecelerating;
//return self.isDecelerating | self.bounces; //If we want to simultaneous gesture on bounce and scrolling
//Edit 2
return self.isDecelerating || self.contentOffset.y < 0 || self.contentOffset.y > MAX(0, self.contentSize.height - self.bounds.size.height); // #Jordan edited - we don't need to always enable simultaneous gesture for bounce enabled tableViews
}
As we only want to change the default gesture behaviour while the tableView is decelerating.
Now change all 'UITableView's class to your newly created tableViewSubClass and run the project, swipe should work while tableView is scrolling. :]
But the swipe looks a little too sensitive while tableView is scrolling. Let's make the swipe a little restrictive.
SubClass UIScrollView. On the UIScrollView subclass implement another UIGestureRecognizer's delegate method gestureRecognizerShouldBegin:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:self];
if (abs(velocity.y) * 2 < abs(velocity.x)) {
return YES;
}
}
return NO;
}
We want to make the "swipe is clearly horizontal". Above code only permits gesture begin if the gesture velocity on x axis is double than on y axis. [Feel free to increase the hard coded value "2" if your like. The higher the value the swipe needs to be more horizontal.]
Now change the `UiScrollView' class (which has multiple TableViews) to your ScrollViewSubClass. Run the project. :]
I've made a project on gitHub https://github.com/rishi420/SwipeWhileScroll
Although apple doesn't like this method too much:
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.
I've found a great way to accomplish this.
This is a complete solution for the problem. In order to scroll the UIScrollView while your UITableView is scrolling you'll need to disable the interaction you have it.
- (void)viewDidLoad
{
[super viewDidLoad];
_myScrollView.contentSize = CGSizeMake(2000, 0);
data = [[NSMutableArray alloc]init];
for(int i=0;i<30;i++)
{
[data addObject:[NSString stringWithFormat:#"%d",i]];
}
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[self.view addGestureRecognizer:tap];
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
[_myTableView setContentOffset:_myTableView.contentOffset animated:NO];
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
scrollView.userInteractionEnabled = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
scrollView.userInteractionEnabled = YES;
}
To sum up the code above, if the UITableView is scrolling, set userInteractionEnabled to NO so the UIScrollView will detect the swipe. If the UITableView is scrolling and the user taps on the screen, userInteractionEnabled will be set to YES.
Instead of using UIScrollView as a container for these multiple table views, try using a UIPageViewController.
You can even integrate this into your existing view controller setup as a child view controller (directly replacing the UIScrollView).
In addition, you'll likely want to implement the required methods from UIPageViewControllerDataSource and possibly one or more of the methods from UIPageViewControllerDelegate.
Did you try the methods : directionalLockEnabled of both your table and scroll and set them up to horizontal for one and vertical for the other ?
Edit :
1)
What you want to do is very complicate since the touch wait some time (like 0.1s) to know what your movement will be. And if your table is moving, it will take your touch immediately whatever it is (because it's suppose to be reactive movement on it).
I don't see any other solution for you but to override touch movement from scratch to detect immediately the kind of mouvement you want (like if the movement will be horizontal) but it will be more than hard to do it good.
2)
Another solution I can advise you is to make your table have left and right margin, where you can touch the parent scroll (pages thing so) and then even if your table is scrolling, if you touch here, only your paging scroll will be touched. It's simpler, but could not fit with your design maybe...
Use UIPageViewController and in the -viewDidLoad method (or any other method what best suits your needs or design) get UIPageViewController's UIScrollView subview and assign a delegate to it. Keep in mind that, its delegate property won't be nil. So optionally, you can assign it to another reference, and then assign your object, which conforms to UIScrollViewDelegate, to it. For example:
id<UIScrollViewDelegate> originalPageScrollViewDelegate = ((UIScrollView *)[pageViewController.view.subviews objectAtIndex:0]).delegate;
[((UIScrollView *)[pageViewController.view.subviews objectAtIndex:0]) setDelegate:self];
So that you can implement UIScrollViewDelegate methods with ease. And your UIPageViewController will call your delegate's -scrollViewDidScroll: method.
By the way, you may be obliged to keep original delegate, and respond to delegate methods with that object. You can see an example implementation in ViewPagerController class on my UI control project here
I faced the same thing recently. My UIScrollview was on paging mode and every page contained a UITableView and like you described it worked but not as you'd expected it to work. This is how solved it.
First I disabled the scrolling of the UIScrollview
Then I added a UISwipeGestureRecognizer to the actual UITableView for left and right swipes.
The action for those swipes were:
[scroll setContentOffset:CGPointMake(currentPointX + 320, PointY) animated:YES];
//Or
[scroll setContentOffset:CGPointMake(currentPointX - 320 , PointY) animated:YES];
This works flawlessly, the only down side is that if the user drags his finger on the UITableVIew that will be considered as a swipe. He won't be able to see half of screen A and half of screen B on the same screen.
You could subclass your scroll view and your table views, and add this gesture recognizer delegate method to each of them...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
I can't be sure this is exactly what you are after, but it may come close.

Two uitable into one uiview

So this is related to this question I asked not too long ago and I'm trying to figure out if the solution I came up with is feasible, I would try it but I'm not really sure how to start
Load a TableViewController on an animated UIView
So what I wanted to do is to have two different results when a animated uiview pops up.
I have two buttons. One button will have a uitable that shows a label and 3 icons, while the other will show one label and a different icon.
I'm using storyboards (since that's the only way I know how to do it) to connect a "Custom UitableCell" to the table.
I dragged two uitableviews and force them to be hidden from the view when viewdidload happens. So I place a prototype cell on each uitableview specific to what objects I need displayed when it gets loaded.
Now I add the uitabledelegate and datasource on the viewcontroller file.
Now my issue is, if I do this on the viewcontroller, can it only communicate to one uitable and not two? (eg. didselectrow, numberofrowsinsection, cellforrowatindexpath, etc) Or can I do and "if else" to figure out which uitable view it is communicating (or loading info) with?
This is the tutorial I followed for th uiview animation
Thoughts?
Yes, your view controller can be the data source and delegate of two uitableview or more.
Make your two UITableView properties of the view controller. Then in your delegate and data source methods, you can check the tableView parameter passed in each method, for example:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (tableView == self.firstTableView) {
return 1; //for the firstTableView
} else {
return 2; //this will be for the secondTableView
}
}
Do the same for your other data source and delegate methods.

How to scroll two UITableView toghether vertically

I have two UITableView. I need to scrolling toghether vertically.
I tried to insert both tables inside a UIScrollView and disabling scrolling of UITableView.
It works but the UITableView which is below is cut it seems as if the table size was set to the maximum size of the screen).
The UITableView i put them inside the view through the storyboard.
I tried to increase the frame of the table within the viewDidLoad method but does not work. If I insert it inside the method viewDidAppear works but it makes me a failure when I click on a UICell and then came back.
Any suggest?
you should work with sections. But if this is not an option for what ever reason you could listen to the scrollViewDidScroll: delegate method and set the offset of the two tables as appropriate. Small Example:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.firstTable) {
self.secondTable.contentOffset = scrollView.contentOffset;
} else {
self.firstTable.contentOffset = scrollView.contentOffset
}
}
you might need to store the touched table in an ivar to determine which scrollevents are initialized by user-interaction and which by your code to avoid 'scroll'-loops.
EDIT:
I might misunderstood your question. You want them to behave as one Table?

UIScrollViews all scroll in the same time

I have a screen with several scrollViews. How to achieve this:
When i tap on one and swipe they all start to scroll. I of course know UIScrollViewDelegate methods and what i trying to do so far is combine -setContentOffset:animated: with scrollViewDidScroll and it works but only for one case - when i start scrolling with delegate scrollview.
How to dynamically change delegate? depends which scroll view user select?
Keep an array of all of your UIScrollView objects. Make sure all of their delegates point to the same object (or if that's not possible, there is some sort of handler that gets called on scrollViewDidScroll). Then use setContentOffset to adjust the offsets. You had the right idea, but you just want to make sure all scroll views except the current view (which is determined by the delegate method argument) is scrolling.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
for (UIScrollView *view in self.scrollViews) {
if (scrollView != view) {
[view setContentOffset:scrollView.contentOffset];
}
}
}

PullToRefresh TableView inside UIViewController

I've got a UIViewController which is also the UITableViewDelegate, amongst other things, for a UITableView, created in FirstView.xib
#interface FirstViewController : UIViewController <
UITextFieldDelegate,
UITableViewDelegate,
UITableViewDataSource
> {
UITableView *searchResults; // this is the property for the table view
...
}
I want this table view to make use of PullToRefresh: https://github.com/leah/PullToRefresh, but the documentation there only explains how to make use of the class as a sub class of the view controller
#import "PullRefreshTableViewController.h"
#interface DemoTableViewController : PullRefreshTableViewController {
NSMutableArray *items;
}
My app uses a Tab bar as the root view controller, can anyone explain to me how I can make the UITableView into a PullRefreshTableView? When I don't have a UITableViewController to edit?
The secret is in the scroll view delegate methods which you can already respond to since you are acting as the tables delegate. This article provides a good start to create your own pull to refresh.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
This will let you know when the user starts dragging the scrollview so you can begin checking whether to refresh or not.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
This allows you to make necessary transitions while scrolling (mainly swapping text and flipping the arrow)
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
This is where you decide if the user has dragged far enough down to start the refresh.
Use this API
It works fine with UIViewController