繁体   English   中英

节错误Swift中的行数

[英]Number of rows in section error Swift

我有一个带有两个自定义单元xib的tableview。

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

一旦点击了someTagsArray ,就会附加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()
}

感谢你们。

尝试使用此代码插入行:

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()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM