Skip to content

Instantly share code, notes, and snippets.

@cmoulton
Created October 17, 2015 20:51
Show Gist options
  • Save cmoulton/d5521a9845cd561d4ca1 to your computer and use it in GitHub Desktop.
Save cmoulton/d5521a9845cd561d4ca1 to your computer and use it in GitHub Desktop.
Demo for programmatic tableview scrolling. See http://grokswift.com/programmatic-uitableview-scrolling/
import UIKit
class ScrollDemoViewController: UITableViewController {
var objects = [AnyObject]()
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let scrollOptionsButton = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: "showScrollOptions")
self.navigationItem.rightBarButtonItem = scrollOptionsButton
for number in 1...100 {
self.objects.append(String(number))
}
}
// MARK: - Table View
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
if let object = objects[indexPath.row] as? String {
cell.textLabel!.text = object
}
else {
cell.textLabel!.text = nil
}
return cell
}
func showScrollOptions() {
let sheet = UIAlertController(title: "Where to", message: "Where would you like to scroll to?", preferredStyle: .ActionSheet)
// Cancel
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler:{ (alert: UIAlertAction) in
self.dismissViewControllerAnimated(true, completion: nil)
});
sheet.addAction(cancelAction)
// Scroll to Top
let firstRowAction = UIAlertAction(title: "First Row", style: UIAlertActionStyle.Default, handler:{ (alert: UIAlertAction) in
self.scrollToFirstRow()
self.dismissViewControllerAnimated(true, completion: nil)
});
sheet.addAction(firstRowAction)
// Last Row
let lastRowAction = UIAlertAction(title: "Last Row", style: UIAlertActionStyle.Default, handler:{ (alert: UIAlertAction) in
self.scrollToLastRow()
self.dismissViewControllerAnimated(true, completion: nil)
});
sheet.addAction(lastRowAction)
// Selected Row
let selectedRowAction = UIAlertAction(title: "Selected Row", style: UIAlertActionStyle.Default, handler:{ (alert: UIAlertAction) in
self.scrollToSelectedRow()
self.dismissViewControllerAnimated(true, completion: nil)
});
sheet.addAction(selectedRowAction)
// Above Header
let topAction = UIAlertAction(title: "Header", style: UIAlertActionStyle.Default, handler:{ (alert: UIAlertAction) in
self.scrollToHeader()
self.dismissViewControllerAnimated(true, completion: nil)
});
sheet.addAction(topAction)
self.presentViewController(sheet, animated: true, completion: nil)
}
func scrollToFirstRow() {
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}
func scrollToLastRow() {
let indexPath = NSIndexPath(forRow: objects.count - 1, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
}
func scrollToSelectedRow() {
let selectedRows = self.tableView.indexPathsForSelectedRows
if let selectedRow = selectedRows?[0] as? NSIndexPath {
self.tableView.scrollToRowAtIndexPath(selectedRow, atScrollPosition: .Middle, animated: true)
}
}
func scrollToHeader() {
self.tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment