Wayne Filkins Wayne Filkins - 5 months ago 19x
Swift Question

Autoscroll to tableviewcell on touch

I was wondering if it is possible to autoscroll a tableview on touch? Basically if you have several cells and you're scrolling through them, and you have half of each cell on the screen and you touch one of them, I want it to scroll so that the one you touch gets centered on the screen. The reason for this is so that if you have buttons and they touch a button while mid-scroll between cells, it would bring the cell with the button to the center of the screen so they could see it.


I suppose that your using something like myArray.count to define the amount of cells in your numberOfRowsInSection method. So, if you wanna scroll to the cell of the middle position, you can use myArray.count/2 to find that the number of the row.

 NSIndexPath* ip = [NSIndexPath indexPathForRow:myArray.count/2 inSection:0];
[self.myTable scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:YES];

Swift version:

var ip: NSIndexPath = NSIndexPath(forRow: myArray.count / 2, inSection: 0)
self.myTable.scrollToRowAtIndexPath(ip, atScrollPosition: .Top, animated: true)

Hope that helps!