gpt4 book ai didi

ios - 将行添加到静态 UITableView : Getting NSRangeException

转载 作者:行者123 更新时间:2023-11-29 01:55:56 25 4
gpt4 key购买 nike

我有一个带有两个部分静态 UITableView。当用户点击第 1 部分中索引 2 处的单元格时,需要在点击的单元格下方添加一个 UITableViewCell。随后每次点击单元格都会导致在任何已添加的单元格下方添加另一个单元格。

这是我到目前为止所尝试过的,但我因 NSRangeException 崩溃,指出索引 6 超出了可用范围,所以我显然没有以某种方式正确更新行数..

根据 Adding unknown number of rows to 'Static Cells' UITableView我已经重写了所有使用 indexPath 的 UITableView 方法,但它们的情况与我的略有不同。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 1 {
// subTaskRow is an array holding all the indexes of the added cells
// so I know where to add the next one. I also know how many I've added
return 6 + subTaskRow.count
} else {
return super.tableView(tableView, numberOfRowsInSection: section)
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// If the Sub Task cell is picked
if (indexPath.section == 1 && indexPath.row == 3) {
// If this is the first subtask we want it to appear under Sub Tasks
// We know the index of the cell below sub task so we can add it in directly
if (subTaskRow.count == 0) {
subTaskRow.addObject(5)
tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 5, inSection: 1)], withRowAnimation: .Automatic)
tableView.reloadData()
// Other sub tasks should appear below previous sub tasks
} else {
// The last object in sub tasks will always be the
// indexPath of the previous added cell
let row = (subTaskRow.lastObject as! Int) + 1
subTaskRow.addObject(row)
tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: 1)], withRowAnimation: .Automatic)
tableView.reloadData()
}
}

tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

// Now we override all the other methods to ensure we don't get a crash
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}

override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return .None
}

override func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int {
return super.tableView(tableView, indentationLevelForRowAtIndexPath: indexPath)
}

我的猜测是,在部分中间添加单元格不会更新现有单元格的索引路径或其他内容?不过,我不确定该怎么做。

最佳答案

我让它工作了,但是这段代码只使用了一个部分:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// subTaskRow is an array holding all the indexes of the added cells
// so I know where to add the next one. I also know how many I've added
return 6 + subTaskRow.count
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// If the Sub Task cell is picked
if (indexPath.section == 0 && indexPath.row == 3) {
// If this is the first subtask we want it to appear under Sub Tasks
// We know the index of the cell below sub task so we can add it in directly
if (subTaskRow.count == 0) {
subTaskRow.append(5)
tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 5, inSection: 0)], withRowAnimation: .Automatic)
tableView.reloadData()
// Other sub tasks should appear below previous sub tasks
} else {
// The last object in sub tasks will always be the
// indexPath of the previous added cell
let row = subTaskRow.last! + 1
subTaskRow.append(row)
tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: 0)], withRowAnimation: .Automatic)
tableView.reloadData()
}
}

tableView.deselectRowAtIndexPath(indexPath, animated: true)
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

cell.textLabel!.text = "the row: \(indexPath.row)"

return cell
}

试试这个代码,看看它是否适合你。从这里开始,您只需添加代码来处理部分,这意味着将代码添加到 numberOfSectionsInTableView。你不应该在这里调用 super.numberOfRowsInTableview 因为如果你有 2 部分数据,你应该知道每部分有多少行

关于ios - 将行添加到静态 UITableView : Getting NSRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30855032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com