我有一个带有两个自定义单元xib的tableview.
第一个xib只包含uilabel,第二个只包含uibutton.
一旦点击了uibutton,就会附加someTagsArray(我在numberOfRows函数中用于计数的数组),并且应该插入新行,但是我得到了这个令人讨厌的错误
Invalid update: invalid number of rows in section 0. The number of
rows contained in an existing section after the update (8) must be
equal to the number of rows contained in that section before the
update (4), plus or minus the number of rows inserted or deleted from
that section (0 inserted, 0 deleted) and plus or minus the number of
rows moved into or out of that section (0 moved in, 0 moved out).
这是我的代码(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()
}
感谢你们.
最佳答案 尝试使用此代码插入行:
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()
}