将行中间与UITableView中间对齐

时间:2015-08-12 17:02:46

标签: ios swift cocoa-touch

我有一个UITableView,它应该在首次显示时显示中间的特定行。在viewDidLoad中,我使用scrollToRowAtIndexPath来实现此目的:

// Center vertically on current row
let scrollIndexPath = NSIndexPath(forRow: currentRow, inSection: 0)
tableView.scrollToRowAtIndexPath(scrollIndexPath, atScrollPosition: UITableViewScrollPosition.Middle, animated: false)

这使得当前行的顶部与表视图的中间对齐,这意味着该行的内容看起来比它应该低。如何使行的中间与表格视图的中间对齐?

1 个答案:

答案 0 :(得分:0)

UITableView是UIScrollView的子类,因此您可以使用:

scrollRectToVisible:<#(CGRect)#> animated:<#(BOOL)#>

并自己计算CGRect,这样你就可以获得比使用更多的控制:

scrollToRowAtIndexPath