In my calendar app, I want my UITableView to scroll to a specific section based on which date is tapped. Current implementation is below:
- (void)calendarDidDate
Swift version of @black-sheep answer:
let sectionIndexPath = IndexPath(row: NSNotFound, section: section)
Swift 3.0
DispatchQueue.main.async {
let indexPath = IndexPath(item: 'item', section: 'section')
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
You have to find/calculate the section
and row
that you want to scroll
NSInteger row = ??;//you have to find/calculate which row you want to show
NSInteger section = ??;//you have to find/calculate which row of the sections you want to show
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[self.agendaTable scrollToRowAtIndexPath:ip
atScrollPosition:UITableViewScrollPositionNone
animated:YES];
When your section has no cells, for example it has just a header or footer, you can use one of the following options:
let sectionRect = yourTable.rect(forSection: sectionNumber)
yourTable.setContentOffset(CGPoint(x: sectionRect.origin.x, y: sectionRect.origin.y), animated: true)
or
let sectionRect = yourTable.rect(forSection: sectionNumber)
yourTable.scrollRectToVisible(sectionRect, animated: false)
Row is must be int value and that variable define in .h and use this variable in anywhere in .m class that you are want to scroll on particular position.
You can scroll UITableView on particular section and also you can scroll to section's particular index cell using following code:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[MytblView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
Try this
[NSIndexPath indexPathForRow:NSNotFound inSection:section]