Obj-c - Scroll to bottom of tableview when view has loaded? - ios

No matter what I do, I cannot get my tableview to scroll to the very bottom when a user gets to my viewcontroller. I'm currently using the following code to try and accomplish this:
ViewController.m
-(void)viewDidAppear {
if (self.tableView.contentSize.height > self.tableView.frame.size.height)
{
CGPoint offset = CGPointMake(0, self.tableView.contentSize.height - self.tableView.frame.size.height);
[self.tableView setContentOffset:offset animated:YES];
}
}
I've also tried pasting this in viewDidLoad, as well as viewWillAppear, and no dice whatsoever. And yes, I am using autolayout. Any ideas?

Related

Scrolling UITableView to specific position using setContentOffset not working properly - ObjC

I saw many answers in SO which describes how can I scroll UITableView to the specific position.
I use setContentOffset for scrolling to particular position.
I meet a weird issue. If my tableView is half scrolled then setContentOffset is working properly. But when I scroll to end of the tableView and then use setContentOffset, table view scrolls a little bit more.
My setContentOffset value is CGPoint(0,199).
[self.tableView setContentOffset:CGPointMake(0,199) animated:NO];
After reaching a bottom I use setContentOffset. Then I check the contentOffset of UITableView. and it is 169.
I am not able to figure it out what exactly the issue is.
Edit::
I am using the following code. When a button in the last cell is pressed:
- (void)userPressedSubmitButtonOnLastCell
{
[self updateData];
[self.tableView reloadData];
[self performSelector:#selector(scrollTableView) withObject:nil afterDelay:0.5];
}
- (void)scrollTableView
{
[self.tableView setContentOffset:CGPointMake(0,199) animated:NO];
}
Why don't you use tableView scrollToRowAtIndexPath: or tableView scrollRectToVisible:(CGRect)? This will allow you to scroll to either specific row or rectangle. ContentOffset is more suitable for UIScrollView.

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

Fix a UIView(from storyboard) to the top of a UITableView

I've attached a UIView from the storyboard to the top of my UITableView and linked it to the code as IBOutlet, and I want it to be fixed to the top of my tableView. I've tried several ways:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
headView.center = CGPointMake(headView.center.x + scrollView.contentOffset.x, headView.center.y + scrollView.center.y);
[scrollView bringSubviewToFront:headView];
}
and
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGRect newFrame = headView.frame;
newFrame.origin.y = 0;
[headView setFrame:newFrame];
}
and some other ways I found in the site, but none worked for me, I affraid because I connected headView from the storyboard.
How can I do it? many thanks!
Sounds like you have a UITableViewController and have added the view to the table views header view, hence it scrolls with the table.
If this is the case try changing your main view controller to a UIViewController and add the view and tableview there.

How to implement scrollViewDidScroll: method?

I'm building an article reading app.List of articles is in UITableViewController,I'm facing an issue when i use scrollViewDidScroll: method first cell of table is display under the UINavigationBar.But when commented this code it displays fine means first cell exactly after the UINavigationBar.
here is my code:
- (void)scrollViewDidScroll: (UIScrollView*)scroll {
// UITableView only moves in one direction, y axis
CGFloat currentOffset = scroll.contentOffset.y;
CGFloat maximumOffset = scroll.contentSize.height - scroll.frame.size.height;
// Change 10.0 to adjust the distance from bottom
if (maximumOffset - currentOffset <= -60.0) {
x++;
[self LoadMore];
[self.tableView addInfiniteScrollingWithActionHandler:^{
}];
[self.tableView reloadData];
}
}
This answer may not be the solution to your problem. This is to point out a major flow in your code that could be the cause of your problem.
You should not reload data [self LoadMore];[self.tableView reloadData]; directly inside scrollViewDidScroll: method. This will cause the method to be called multiple times as you scroll.
And I think you have implemented infinite loading incorrectly.
This code should be placed inside some initializer method (e.g., viewDidLoad)
[tableView addInfiniteScrollingWithActionHandler:^{
[self LoadMore];
[self.tableView reloadData];
// call [tableView.infiniteScrollingView stopAnimating] when done
}];
If you are planing to load more items as you scroll, I don't think you need to use scrollViewDidScroll: at all. Simply put the above code inside viewDidLoad: and everything should work. You will have to keep track of the number of items loaded separately. Hope this will help. :)

Scroll UITableView using second UITableView

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!

Resources