【问题标题】:Scrolling to last row of table view only scrolls half way滚动到表格视图的最后一行只滚动一半
【发布时间】:2015-04-08 21:19:32
【问题描述】:

在用户向提要添加评论后,我试图滚动到表格视图的最后一行:

self.loadDataFromParse();

// load the last row
let numberOfSections = self.tableView.numberOfSections()
let numberOfRows = self.tableView.numberOfRowsInSection(numberOfSections-1)

if numberOfRows > 0 {
     println(numberOfSections)
     let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
     self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
}

tableview 只能滚动到大约一半/ 3/4。 任何意见表示赞赏

【问题讨论】:

  • 我会得到该 indexPath 处单元格的UITableViewCell。看看它是否是最后一个单元格。
  • 不太清楚你的意思,你会怎么做?
  • UITableViewCell cell = self.tableView.cellForRowAtIndexPath(indexPath) 检查有关此单元格的信息。看看它是否确实是表格中的最后一个单元格。因为它只滚动了 3/4,所以这似乎是第 3/4 个单元格吧?
  • 谢谢,试过了,我得到的行数 = 21,但是对于单元格:如果让单元格:TimelineCell = self.tableView.cellForRowAtIndexPath(indexPath)?作为? TimelineCell{ println("最后一行 cmets: (cell.postLabel?.text)"); ......我得到零......

标签: uitableview swift scroll


【解决方案1】:

此解决方案可能对遇到相同问题的其他人有所帮助。 这个问题是一个错误,请参阅讨论here

我使用页脚视图将 cmets 添加到提要中,因此我在这里使用了它的高度,并且 tableview 将滚动到页脚视图正上方的最后一行:

self.tableView.reloadData();

            // load the last row


            let offset = CGPoint(x: 0, y: self.tableView.contentSize.height - self.tableView.frame.size.height + self.FOOTERHEIGHT);

            self.tableView.contentOffset = offset;

【讨论】:

  • 在设置偏移量之前调用 self.tableView.layoutIfNeeded() 也可以解决问题,reloadData 会变得很昂贵。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
相关资源
最近更新 更多