开发者

Is there a suggested way of how to let a UITableView start displaying at a specific location?

I have a navigation-based app like this:

There is a table view with an overview of all chapters. If the user taps a chapter, I push another view controller on the stack which shows the contents of the book. It's a small manual book with not much text and no images. Every chapter is displayed in one cell, and I kno开发者_StackOverfloww the exact contentOffset.y for each chapter.

Problem: I can't make it start with the tapped chapter.

In the initialization method of my ReadingTableView, I set the desired contentOffset like this:

self.contentOffset = CGPointMake(self.contentOffset.x, targetContentOffsetY);

I also tried:

[self setContentOffset:CGPointMake(self.contentOffset.x, targetContentOffsetY) animated:NO];

The table view JUMPS to that desired offset, like if it was badly animated. You can see for a fraction of a second a blank table view, and then the whole thing very quickly moves up with animation.


You've mentioned you are setting contentOffset at initialization method, since the UITableView is not actually loaded at your init method, the content size is not set neither. That's why the animation did not have effect.

If you are using an UIViewController or UITableViewController, try "setContentOffset:animated:" method in "viewDidAppear:" (careful with the Did word, not Will) method.

Also, "scrollToRowAtIndexPath:atScrollPosition:animated:" is better for scrolling an UITableView.

P.S.: Animation may also looks like a JUMP If your table cell loads very very slow, if you are not sure where is the problem, you can temporarily use an empty cell for test.


Set the contentOffset in viewWillAppear of the containing UIViewController. [UITableView scrollToRowAtIndexPath: is useful too, if you are trying to offset to a specific row instead of just a number of pixels. Again, fire scrollToRowAtIndexPath in viewWillAppear or any time after your tableView has loaded it's data.


Why not:

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

Give it the indexPath of the specific cell.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜