我有一个带有两个自定义单元格的桌面视图。
第一个xib只包含uilabel,第二个只包含xib按钮。
单击in按钮后,将追加someTagsArray ( numberOfRows函数中用于计数的数组),并插入新的行,但是我得到了这个严重的错误
无效更新:第0节中的行数无效。更新(8)后包含在现有节中的行数必须等于更新(4)之前包含在该节中的行数,加上或减去从该节插入或删除的行数(0插入,0删除),加上或减去移动到或移出该节的行数(0移动,0移出)。
这是我的代码(numberOfRowsInSection)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->
Int {
return someTagsArray.count + 1
}cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.row < someTagsArray.count){
var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
cell.lblCarName.text = linesmain["start"]![indexPath.row]
return cell
} else {
var celle:vwAnswers = self.tableView.dequeueReusableCellWithIdentifier("cell2") as! vwAnswers
celle.Answer1.setTitle(answersmain["start"]![0], forState:UIControlState.Normal)
// answertitle is a global string variable
answertitle1 = "\(celle.Answer1.currentTitle!)"
return celle
}}最后,崩溃应用程序的函数代码
func insertData(){
// appending the array to increase count
someTagsArray += linesmain[answertitle1]!
tableView.beginUpdates()
let insertedIndexPathRange = 0..<self.linesmain[answertitle2]!.count-4
var insertedIndexPaths = insertedIndexPathRange.map { NSIndexPath(forRow: $0, inSection: 0) }
tableView.insertRowsAtIndexPaths(insertedIndexPaths, withRowAnimation: .Fade)
tableView.endUpdates()
}谢谢你们。
发布于 2015-08-27 13:21:17
尝试以下插入行的代码:
func insertData(){
let initialCount = someTagsArray.count as Int
let newObjects = linesmain[answertitle1] as! NSArray
// appending the array to increase count
//someTagsArray += newObjects
someTagsArray.addObjectsFromArray(newObjects)
self.tableView!.beginUpdates()
var insertedIndexPaths: NSMutableArray = []
for var i = 0; i < newObjects.count; ++i {
insertedIndexPaths.addObject(NSIndexPath(forRow: initialCount+i, inSection: 0))
}
self.tableView?.insertRowsAtIndexPaths(insertedIndexPaths as [AnyObject], withRowAnimation: .Fade)
self.tableView!.endUpdates()
}https://stackoverflow.com/questions/32246351
复制相似问题