gpt4 book ai didi

ios - 部分错误 Swift 中的行数

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:44 24 4
gpt4 key购买 nike

我有一个带有两个自定义单元格 xib 的表格 View 。

第一个 xib 仅包含 uilabel,第二个仅包含 uibutton。

单击 ui 按钮后,将附加 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()
}

关于ios - 部分错误 Swift 中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32246351/

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