[英]Can not scroll to row at index path in UITableView
I am trying to scroll to index path when I click on collection view cell in a different view, with the help of observer pattern with notification, and get exception that my bound for row and section are 0 (while the table IS populated properly). 当我在具有通知的观察者模式的帮助下,在其他视图中单击集合视图单元格时,我尝试滚动到索引路径,并获得例外,即我的行和节的绑定为0(而表格已正确填充)。
func selectedDay(notification: NSNotification) {
let userInfo = notification.userInfo as! [String: AnyObject]
let selectedDayIndex = userInfo["selectedDayIndex"] as! Int?
if let selectedDay = selectedDayIndex {
print("YES: \(selectedDay)")
self.tableView.reloadData()
self.scrollToRow(selectedDay)
}
}
func scrollToRow(section: Int) {
let indexPath = NSIndexPath(forRow: 0, inSection: section)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Middle, animated: true)
}
I am able to print the index I am getting from the notification, and my table is populated like this: 我可以打印从通知中获取的索引,并且我的表按以下方式填充:
I understand that my table seems to be empty, when accessing it from this method. 我了解从此方法访问它的表似乎是空的。 I do have proper delegate, data source connections.
我确实有适当的委托,数据源连接。 This is my log:
这是我的日志:
cell for row at index is loaded from the array, that is fetched with completion block, and table.reloadData() on return of boolean true: 从数组加载在索引处的行的单元格,该数组通过完成块获取,并在返回布尔值true时使用table.reloadData():
This line: 这行:
let indexPath = NSIndexPath(forRow: 0, inSection: section)
assumes there is an object at meetings[0][section]. 假设在Meetings [0] [section]中有一个对象。 You should say:
你应该说:
func scrollToRow(section: Int) {
if(meetings.count > 0 && meetings[section].count > 0)
{
let indexPath = NSIndexPath(forRow: 0, inSection: section)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}else{
print("there is no section: \(section)")
}
} }
Ok, just simple check helped, because indeed some of the sections were not populated: 好的,只是简单的检查就可以了,因为确实没有填充某些部分:
func scrollToRow(section: Int) {
if(meetings.count > 0 && meetings[section].count > 0)
{
let indexPath = NSIndexPath(forRow: 0, inSection: section)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}else{
print("there is no section: \(section)")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.