当我在另一个视图中单击集合视图单元格时,我试图滚动到索引路径,在带有通知的观察者模式的帮助下,并获得我的行和节的边界为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)
}
答案 0 :(得分:1)
这一行:
let indexPath = NSIndexPath(forRow: 0, inSection: section)
假设会议[0] [部分]中有一个对象。你应该说:
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)")
}
}
答案 1 :(得分:0)
好的,只是简单的检查有帮助,因为确实有些部分没有填充:
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)")
}
}