首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >区段中的行数

区段中的行数
EN

Stack Overflow用户
提问于 2015-08-27 10:01:45
回答 1查看 4.6K关注 0票数 4

我有一个带有两个自定义单元格的桌面视图。

第一个xib只包含uilabel,第二个只包含xib按钮。

单击in按钮后,将追加someTagsArray ( numberOfRows函数中用于计数的数组),并插入新的行,但是我得到了这个严重的错误

无效更新:第0节中的行数无效。更新(8)后包含在现有节中的行数必须等于更新(4)之前包含在该节中的行数,加上或减去从该节插入或删除的行数(0插入,0删除),加上或减去移动到或移出该节的行数(0移动,0移出)。

这是我的代码(numberOfRowsInSection)

代码语言:javascript
复制
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->    
Int {
        return someTagsArray.count + 1
    }

cellForRowAtIndexPath

代码语言:javascript
复制
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

        }}

最后,崩溃应用程序的函数代码

代码语言:javascript
复制
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()
}

谢谢你们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-27 13:21:17

尝试以下插入行的代码:

代码语言:javascript
复制
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()
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32246351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档