Scroll UITableView using second UITableView - ios

I have an application, that uses external display.
I have real table view on my iPad, and mirror table view on external screen.
Now, I'm using NSNotification center to notify table on external view that it must scroll, but scrolling is very rough, visually not sexy with lags.
How can i improve the performance of this? How to make it smooth and sexy?
Yes, both TableViews have different sizes.
Here is my code:
Controller with real UITableView:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == self.iboTable)
[[NSNotificationCenter defaultCenter] postNotificationName:#"remoteControlTableScrolled" object:scrollView];
}
And the controller that is on external screen:
- (void)TableScrolled:(NSNotification *) notification
{
UITableView *notificationTableView = notification.object;
if(notificationTableView.contentOffset.y>self.iboTable.contentSize.height - self.iboTable.bounds.size.height)
{
CGFloat yOffset = 0;
yOffset = self.iboTable.contentSize.height - self.iboTable.bounds.size.height;
[self.iboTable setContentOffset:CGPointMake(0, yOffset) animated:YES];
}
else if(notificationTableView.contentOffset.y + notificationTableView.frame.size.height == notificationTableView.contentSize.height)
{
[self.iboTable scrollRectToVisible:CGRectMake(0, self.iboTable.contentSize.height - self.iboTable.bounds.size.height, self.iboTable.bounds.size.width, self.iboTable.bounds.size.height) animated:YES];
}
else
{
[self.iboTable setContentOffset:CGPointMake(notificationTableView.contentOffset.x,notificationTableView.contentOffset.y)animated:YES];
}
}

Notifications are a clumsy mechanism for posting so many of them at such short periods.
Have a reference of your external screen view controller in your main screen view controller, and in scrollViewDidScroll: set the content offset accordingly.

Found a problem, in this line:
[self.iboTable setContentOffset:CGPointMake(notificationTableView.contentOffset.x,notificationTableView.contentOffset.y)animated:YES];
animated: NO;
animation leads to lags and not smooth scrolling, because it is used to animated scroll ONCE, for example till the 10th element!

Related

Scrolling to bottom of tableView programatically when uibutton is tapped

When my View (containing a Table View) loads, I want it to scroll to the bottom of the table view. This works great. However, when one of my buttons (sendReply) is tapped, I also want the tableView to scroll to the bottom. For some reason, scrolling to the bottom of the tableView works when the View is initially loaded, however [self bottomScroll:self] doesn't seem to fire when I place it inside of my sendReply action?
.m
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self bottomScroll:self];
}
- (void)bottomScroll:(id)sender {
if (self.messages.count > 0)
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.messages.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
- (IBAction)sendReply:(id)sender {
[self bottomScroll:self];
}
This should work. Are you sure messages.count is greater than 0 when you tap the button? You can set a breakpoint at that line and see if this code is executed.
Also you can try -scrollRectToVisible as an alternative.
as phi stated scrollRectToVisible would be the way I would go. Call your tableView's scroll view passing in the rect of your button you want to show. It's been awhile since I've used obj-c but:
-(IBAction)sendReply: (id)sender {
[scrollView scrollRectToVisible: sendReply.frame, animated: YES]
}
? The syntax may be wrong there but it should be close enough to get you where you are going.
In swift:
#IBAction func sendReply(sender: UIButton/AnyObject) -> Void {
let scrollView = tableView.scrollView
scrollView.scrollrectToVisible(_ rect: sender.frame, animated: true)
}
It seems strange based on your description. Need more code to find whats going on. What can be inferred is you want to scroll the last cell to the bottom. So make sure the contentsize of tableview is larger than its frame vertically.
Try this one
if (self.tableView.contentSize.height > self.tableView.frame.size.height){
CGPoint point = CGPointMake(0, self.tableView.contentSize.height - self.tableView.frame.size.height);
[self.tableView setContentOffset:point];
}

Making two UIScrollView's scroll as one, or transferring inertia between scroll views

In our app you can build a question by searching for options from multiple third party sources. Most of these search results are displayed as full-width tableview cells, as their data suits that format (has a bunch of metadata text I can display next to the thumbnail).
In the case of images however, a collection view makes much more sense. But now I run into the problem of a vertical scrolling tableview containing a vertical scrolling collection view.
http://www.screencast.com/t/7Z48zkkW
I can make it work somewhat by capturing the viewDidScroll on the collection view and updating the parent scroll view instead at the appropriate offsets, but it only works well when the user is actively dragging the collection view.
self.collectionVC.scrollViewDidScroll = ^(UIScrollView *scrollView) {
#strongify(self);
if (self.tableView.contentOffset.y < self.scrollingHeightOffset && scrollView.contentOffset.y > 0) {
CGFloat maxheight = MIN(self.scrollingHeightOffset, self.tableView.contentOffset.y + scrollView.contentOffset.y);
self.tableView.contentOffset = CGPointMake(0, maxheight);
scrollView.contentOffset = CGPointMake(0, 0);
} else if (scrollView.contentOffset.y < 0 && self.tableView.contentOffset.y > -topGuide) {
CGFloat minheight = MAX(-topGuide, self.tableView.contentOffset.y + scrollView.contentOffset.y);
self.tableView.contentOffset = CGPointMake(0, minheight);
scrollView.contentOffset = CGPointMake(0, 0);
}
};
When 'flinging' the collection view, the scrolling stops abruptly, losing the inertia of the collection view. Touching the tableview to scroll has a different problem, as I'm not capturing that when it hits the end and scrolling the collection view instead.
Right now the collection view lives in a cell of the tableview, but it could also be a peer if necessary. I'm trying to determine the best way to make these two scrollviews appear as one.
As far as I know you did the right thing. viewDidScroll should be called several times even after user lift the finger and scroll view is returning to some position.
I use exact same approach to scroll couple scrollViews in sync when user is dragging one of them. Works perfectly.
You might want to check your calculation logic inside this method. See if it calls for all changes after user lift a finger, but you're positioning other scroll view in a wrong way.
I did this, let me know if it works for you
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView1.panGestureRecognizer addTarget:self action:#selector(didRecognizePanGesture:)];
[self.tableView2.panGestureRecognizer addTarget:self action:#selector(didRecognizePanGesture:)];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.selectedTableView == scrollView)
{
if (scrollView == self.tableView1)
{
self.tableView2.contentOffset = scrollView.contentOffset;
}
else if (scrollView == self.tableView2)
{
self.tableView1.contentOffset = scrollView.contentOffset;
}
}
}
- (void)didRecognizePanGesture:(UIPanGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
if (self.selectedTableView != nil) //this is gonna make stop the previous scrollView
{
[self.selectedTableView setContentOffset:self.selectedTableView.contentOffset animated:NO];
}
self.selectedTableView = (UITableView*)gesture.view;
}
}

UIScrollView events for scrolling that definitely occurred

I am working with aUIScrollview, and based on if the user scrolls left or right, I reconfigure the UI. The problem is that I need to verify that the user definitely crossed from one screen to another (something along contentOffset).
I've tried using this method:
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
But this will fire if the user kind of moves the scrollview partially in one direction, but then doesn't complete the gesture.
I've also tried this method:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView;
but mainly the same issue; the scrollview scrolls left and right, but on an iPhone, with a content with of 640 ( 320 * 2). I am trying to figure out if the scroll did cross over or not, from one location to the other.
Any suggestions?
I am not entirely sure what you mean by "crossed from one screen to another" I assume you mean paging? If so, here's what you can do:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
int page = sender.contentOffset.x / width;
[self.pageControl setCurrentPage:page]; // in case you need it for updating a UIPageControl
}
I found a way of doing this :
I have a variable to maintain the x location of the contentOffset
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
currX = self.scrollView.contentOffset.x;
}
And then in this event :
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if(self.scrollView.contentOffset.x == currX ) {
return;//This is what I needed, If I haven't panned to another view return
}
//Here I do my thing.

UICollectionView not removing old cells after scroll

I have a UICollectionView with a grid of images. When you tap on one, it opens up the grid and shows a subview with some details. Like this:
I open up the grid in my UICollectionViewLayout by adjusting the UICollectionViewLayoutAttributes and setting a translation on the transform3D property for all cells below the current row of the selected item. This works really nicely, and is a much better animation and a simpler approach than my first attempt at inserting another cell into the grid which is a different size to the others.
Anyway... it works most of the time, but then after continued use I see old images on the collection view. They are like ghost cells. I can't click them, it's like they haven't been removed from the collection view properly, and they sit on top of the cells preventing taps and just being a nuisance. Like this:
Any ideas why these cells are doing this?
EDIT:
I'd like to add, I think it only happens when i scroll the collection view really fast. I've written my own UICollectionViewFlowLayout replacement to test if it still happens. It does.
EDIT 2:
The 3d transforms or layout have nothing to do with this. It must be a bug in UICollectionView. I can exploit by just scrolling really fast, letting come to a standstill and then querying the views that are on screen. There are often double the number of cells, but they are hidden as they are stacked on top of each other. My implementation above reveals them because of the translation i do.
This can really hurt performance too.
See my answer for a solution.
My second edit of my question details why this is happenening, and here is my workaround. It's not bullet proof, but it works in my case, and if you experience somethign similar you could tweak my solution:
- (void) removeNaughtyLingeringCells {
// 1. Find the visible cells
NSArray *visibleCells = self.collectionView.visibleCells;
//NSLog(#"We have %i visible cells", visibleCells.count);
// 2. Find the visible rect of the collection view on screen now
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;
//NSLog(#"Rect %#", NSStringFromCGRect(visibleRect));
// 3. Find the subviews that shouldn't be there and remove them
//NSLog(#"We have %i subviews", self.collectionView.subviews.count);
for (UIView *aView in [self.collectionView subviews]) {
if ([aView isKindOfClass:UICollectionViewCell.class]) {
CGPoint origin = aView.frame.origin;
if(CGRectContainsPoint(visibleRect, origin)) {
if (![visibleCells containsObject:aView]) {
[aView removeFromSuperview];
}
}
}
}
//NSLog(#"%i views shouldn't be there", viewsShouldntBeThere.count);
// 4. Refresh the collection view display
[self.collectionView setNeedsDisplay];
}
and
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
[self removeNaughtyLingeringCells];
}
}
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self removeNaughtyLingeringCells];
}
A quick further comment to bandejapaisa's: under iOS 6 only, I found that UICollectionView also had a habit of bungling animated transitions. The original cells would remain where they were, copies would be made and then the copies would be animated. Usually on top of the originals but not always. So a simple bounds test wasn't sufficient.
I therefore wrote a custom subclass of UICollectionView that does the following:
- (void)didAddSubview:(UIView *)subview
{
[super didAddSubview:subview];
//
// iOS 6 contains a bug whereby it fails to remove subviews, ever as far as I can make out.
// This is a workaround for that. So, if this is iOS 6...
//
if(![UIViewController instancesRespondToSelector:#selector(automaticallyAdjustsScrollViewInsets)])
{
// ... then we'll want to wait until visibleCells has definitely been updated ...
dispatch_async(dispatch_get_main_queue(),
^{
// ... then we'll manually remove anything that's a sub of UICollectionViewCell
// and isn't currently listed as a visible cell
NSArray *visibleCells = self.visibleCells;
for(UIView *view in self.subviews)
{
if([view isKindOfClass:[UICollectionViewCell class]] && ![visibleCells containsObject:view])
[view removeFromSuperview];
}
});
}
}
Obviously it's a shame that 'is this iOS 6' test can't be a little more direct but it's hidden off in a category in my actual code.
A Swift UICollectionView extension version of bandejapaisa's answer:
extension UICollectionView {
func removeNaughtyLingeringCells() {
// 1. Find the visible cells
let visibleCells = self.visibleCells()
//NSLog("We have %i visible cells", visibleCells.count)
// 2. Find the visible rect of the collection view on screen now
let visibleRect = CGRectOffset(bounds, contentOffset.x, contentOffset.y)
//NSLog("Rect %#", NSStringFromCGRect(visibleRect))
// 3. Find the subviews that shouldn't be there and remove them
//NSLog("We have %i subviews", subviews.count)
for aView in subviews {
if let aCollectionViewCell = aView as? UICollectionViewCell {
let origin = aView.frame.origin
if (CGRectContainsPoint(visibleRect, origin)) {
if (!visibleCells.contains(aCollectionViewCell)) {
aView.removeFromSuperview()
}
}
}
}
// 4. Refresh the collection view display
setNeedsDisplay()
}
}

how to detect when i scrolled below the UITableView AND snaps back

im atempting to do a facebook type load more data and right now it works, but very laggy on the device because its asking the server to get anything that isnt there already, then calling a scroll all the way down function (because somehow when i reload the data it scrolls to the top). If there would be a way to prevent scrolling to the top that would be great. But my main thing is, is there a way to detect when i scrolled down, and LET GO (stopped scrolling) as in i scrolled past what i have, then it went back to its possition and then calls my methods... Currently it keeps getting called when the scroll is greater then the height of the UITableView heres the code
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat height = scrollView.frame.size.height;
CGFloat contentYoffset = scrollView.contentOffset.y;
CGFloat distanceFromBottom = scrollView.contentSize.height - contentYoffset;
if(distanceFromBottom < height)
{
[getMessage removeAllObjects];
[self loadMessages];
[self.tableView reloadData];
[self scrollAllTheWayDown];
}
}
UIScrollViewDelegate method
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
does what you want. it gets called after user stops scrolling, and you can check for scrollOffset at that point to see if you should trigger your refresh code. (you'd use scrollViewDidScroll to update the view to show user update will happen if he lets go)

Resources