Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Fix TableView To Bottom Like Messages App

So this is completely out of my area of (expertise?) so I figured I'd ask it and see if anyone more experienced could give me a yes or no answer.

So, I'm using Appcelerator Titanium to build an app that has many tableviews. I am looking to build it so that some of the tableViews start at the bottom and I can scroll UP instead of scrolling DOWN to start, exactly the same way as the "Messages" app does on the iPhone.

The way it works is content loads in and it's automatically loaded in with the tableview fixed to the bottom, then you can scroll upwards to see older posts.

I cannot find a way to do this in appcelerator other than create the tableview, load the data, then scroll it to the bottom (which obviously has that snapping). I can hide the tableview, scroll to bottom, then show the tableview, but again... not ideal.

Now the question... Is it possible using the standard iOS SDK (Not appcelerator) to do set a table to fix to the bottom instead of the top? If not, i'll have to find a work around somehow. If it is, I'd like to try to build it into a titanium module if at all possible...

Anyways, thanks! And hopefully this is a simple answer for some of you.

like image 365
Mike Fogg Avatar asked Dec 27 '22 19:12

Mike Fogg


2 Answers

Actually, it really is quite trivial. Something like this would work just fine:

-(void)viewDidLoad {
   //place this at the bottom of your viewDidLoad method, or in any method that initially reloads the table
    NSIndexPath* ipath = [NSIndexPath indexPathForRow: myArray.count-1 inSection: 0];
    [tableView scrollToRowAtIndexPath: ipath atScrollPosition: UITableViewScrollPositionBottom animated: NO]
}
like image 112
CodaFi Avatar answered Jan 12 '23 18:01

CodaFi


I've tried to scroll in viewDidLoad, but after view controller appears, i can see the the begining of table on view appear.

I've found the better place for the initial scrolling.

- (void) viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    NSInteger lastSectionIndex = MAX(0, [self.chatTableView numberOfSections] - 1);
    NSInteger lastRowIndex = MAX(0, [self.chatTableView numberOfRowsInSection:lastSectionIndex] - 1);
    NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
   [self.chatTableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}
like image 43
Vlad E. Borovtsov Avatar answered Jan 12 '23 17:01

Vlad E. Borovtsov