You can get the indexPath of the last row in last section like this. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(numberOfRowsInLastSection - 1) inSection:(numberOfSections - 1)]; Here, numberOfSections is the value you return from numberOfSectionsInTableView: method.
There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.
add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.
For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.
scrollToRowAtIndexPath
should work.
In viewWillAppear:
, try this:
[theTableView reloadData];
NSIndexPath* ip = [NSIndexPath indexPathForRow:rowNumberHere inSection:sectionNumberHere];
[theTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];
rowNumberHere
is the row number in the data source you want to scroll to.
atScrollPosition
is just one of the values in the UITableViewScrollPosition
enum which can determine where on the screen the row number you want will show up. However, depending on the number of rows and which row you are scrolling to, it may not make a difference.
Putting reloadData:
avoids an exception if the data is not loaded yet in viewWillAppear:
. If you put the scrollToRowAtIndexPath
in viewDidAppear:
, you would not need the reloadData:
but you will see the table jump a little which you say you don't want.
Edit: @Theory, try changing your code as follows...
[tableView reloadData];
int lastRowNumber = [tableView numberOfRowsInSection:0] - 1;
NSIndexPath* ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
[tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];
Please note numberOfRowsInSection
returns row count, not the last row number (which is row count - 1).
Answer of @DyingCactus in Swift 3 & Swift 4:
let lastRow: Int = self.tableView.numberOfRows(inSection: 0) - 1
let indexPath = IndexPath(row: lastRow, section: 0);
self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
You can call -scrollToRowAtIndexPath:atScrollPosition:animated within the -viewWillAppear: method of your TableViewController.
atScrollPosition: allows you to set where you want your cell for rowAtIndexPath to appear. There are four options:
UITableViewScrollPositionTop - puts your cell right at the top of the view
UITableViewScrollPositionMiddle - centers your cell in the view
UITableViewScrollPositionBottom - puts your cell at the bottom
UITableViewScrollPositionNone - Using this setting will position in the cell in user view with minimum scrolling/movement.
The behavior is different in three scenarios :-
If the cell is already in view, it does nothing.
If the cell is above the current view, it scrolls the cell to the top of the view.
If the cell is beneath the current view, it scrolls the cell to the bottom of the view.
I'm using autolayout and none of the answers worked for me. Here is my solution that finally worked:
@property (nonatomic, assign) BOOL shouldScrollToLastRow;
- (void)viewDidLoad
{
[super viewDidLoad];
_shouldScrollToLastRow = YES;
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// Scroll table view to the last row
if (_shouldScrollToLastRow)
{
_shouldScrollToLastRow = NO;
[self.tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With