gooberboobbutt
gooberboobbutt

Reputation: 787

code scroll to next row in table view?

So in my project there is a tableview with each row populated with an image from parse. i was hoping to disable user scroll and have a button scroll down to the next row instead. is this possible?

Figured it out

let button = sender as! UIButton
let view = button.superview!
let cell = view.superview as! <cellname>

let indexPath = tableView.indexPathForCell(cell)

let row = indexPath?.row

tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: row!+1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)

Upvotes: 0

Views: 1887

Answers (6)

Joseph Aguilar
Joseph Aguilar

Reputation: 1

In Swift 3.0 in Xcode 8:

let mylist = MessageList //your class array

tableView1.scrollToRow(at: IndexPath(row: (mylist?.count)!-1, section: 0), at: UITableViewScrollPosition.bottom, animated: true)

Upvotes: 0

gooberboobbutt
gooberboobbutt

Reputation: 787

I found the solution!

let button = sender as! UIButton
let view = button.superview!
let cell = view.superview as! <cellname>

let indexPath = tableView.indexPathForCell(cell)

let row = indexPath?.row

tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: row!+1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)

Upvotes: 0

Hamza Ansari
Hamza Ansari

Reputation: 3084

enter image description here

I have made a sample project on git https://github.com/hammyhamza/StackOverflow

Upvotes: 2

JustAnotherCoder
JustAnotherCoder

Reputation: 2575

You have to use scrollToRowAtIndexPath like so:

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

for indexPath you need to use the NSIndexPath class like so:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowYouDesire inSection:sectionYouDesire];

Upvotes: 0

jinhua liao
jinhua liao

Reputation: 286

Set the property scrollEnabled to NO.because UITableView is a subclass of UIScrollView. then you can control the scroll use your own code.

Upvotes: 0

Tien Dinh
Tien Dinh

Reputation: 1038

Yes, you could use the function scrollToRowAtIndexPath from UITableView to programmatically scroll the table view.

Upvotes: 0

Related Questions