How to scroll UITableView to specific position
Swift 4.2 version:
let indexPath:IndexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .none, animated: true)
Enum: These are the available tableView scroll positions - here for reference. You don't need to include this section in your code.
public enum UITableViewScrollPosition : Int {
case None
case Top
case Middle
case Bottom
}
DidSelectRow:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let theCell:UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath)
if let theCell = theCell {
var tableViewCenter:CGPoint = tableView.contentOffset
tableViewCenter.y += tableView.frame.size.height/2
tableView.contentOffset = CGPointMake(0, theCell.center.y-65)
tableView.reloadData()
}
}
[tableview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
This will take your tableview to the first row.
it should work using - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
using it this way:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[yourTableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
atScrollPosition
could take any of these values:
typedef enum {
UITableViewScrollPositionNone,
UITableViewScrollPositionTop,
UITableViewScrollPositionMiddle,
UITableViewScrollPositionBottom
} UITableViewScrollPosition;
I hope this helps you
Cheers