【发布时间】:2015-05-08 03:26:21
【问题描述】:
无效更新:第 0 节中的行数无效。 更新 (5) 后包含在现有节中的行必须是 等于该节之前包含的行数 update (1),加上或减去插入或删除的行数 该部分(插入 1 个,删除 0 个)并加上或减去数量 移入或移出该部分的行(0 移入,0 移出)。
我正在尝试在用户点击一行时将行添加到表格视图中,以创建可展开的部分,但是在 Xcode 尝试添加它们之前不会计算额外的行,因此会导致此错误 (我想)。谁能指出我正确的方向?
// sectionExpanded is set to false in viewDidLoad. It is set to true when
// the user taps on the expandable section (section 0 in this case)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 && sectionExpanded {
return 5
} else {
return 1
}
}
// This should recount the rows, add the new ones to a temporary array and then add
// them to the table causing the section to 'expand'.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedItem = menu[indexPath.row]
let cell = tableView.cellForRowAtIndexPath(indexPath) as MenuCell
if indexPath.section == 0 {
var rows: Int
var tmpArray: NSMutableArray = NSMutableArray()
sectionExpanded = !sectionExpanded
rows = tableView.numberOfRowsInSection(0)
for i in 1...rows {
var tmpIndexPath: NSIndexPath
tmpIndexPath = NSIndexPath(forRow: i, inSection: 0)
tmpArray.addObject(tmpIndexPath)
}
if !sectionExpanded {
tableView.deleteRowsAtIndexPaths(tmpArray, withRowAnimation: UITableViewRowAnimation.Top)
} else {
tableView.insertRowsAtIndexPaths(tmpArray, withRowAnimation: UITableViewRowAnimation.Top)
}
} else {
delegate?.rightItemSelected(selectedItem)
}
}
【问题讨论】:
标签: ios objective-c xcode uitableview swift