Number of lines in a Swift error line

I have a table view with two xibs for custom cells.

The first xib contains only uilabel, and the second only uibutton.

After clicking the button, a uibutton is added someTagsArray(the array that I use to count in the numberOfRows function) and new lines should be inserted, but instead I get this nasty error

Invalid update: invalid number of lines in section 0. The number of lines contained in an existing section after update (8) must be equal to the number of lines contained in this section before update (4), plus or minus the number of lines inserted or deleted from this section (0 inserted, 0 deleted) and plus or minus the number of rows moved to or from this section (0 moved, 0 displayed).

Here is my code (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

        }}

and finally, the function code that the application breaks

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

Thanks guys.

+4
source share
1 answer

Try this code to insert rows:

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()
}
+3
source

All Articles