ios - Swift,将类数组添加到变量中不会更新原始类中的数组内容

标签 ios arrays swift uitableview

我是 IOS 新手,请原谅我的编码错误。我面临一个问题,我有一个包含两个部分的 tableView Controller 。第一部分有一个按钮,单击时,将数据附加到数组中并删除第一部分中它自己的行(我这样做是因为第一部分中有额外的不相关行)。第二部分中的行数基于 array.count。

我的问题是我尝试了开始/结束更新,但它仍然不起作用。每当我运行下面的代码并运行 startNewDay 函数时(单击按钮时),就会发生此错误:

'试图将第 0 行插入第 1 节,但更新后第 1 节中只有 0 行'

这没有任何意义,因为我在插入新行之前已经附加了数组。在我附加它之前,数组是空的。第二部分中的行数不应该与 array.count 相同吗?

TableView 委托(delegate)代码:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        if dataModel.lists[0].dayHasStarted == false {
            return 2
        } else {
            return 1
        }
    } else {
        if itemDoneCount == dataModel.lists[0].item.count && dataModel.lists[0].doneButtonVisible {
            return dataModel.lists[0].item.count + 1
        } else {
            return dataModel.lists[0].item.count
        }
    }
}

按下 startNewDay 按钮功能:

@IBAction func startNewDayDidPress(sender: AnyObject) {
    dataModel.lists[0].dayHasStarted = true
    dataModel.lists[0].startDate = NSDate()
    addItemButton.enabled = !addItemButton.enabled

    // deleting start new day button

    let indexPath = NSIndexPath(forRow: 1, inSection: 0)
    let indexPaths = [indexPath]

    tableView.beginUpdates()
    tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Fade)
    tableView.endUpdates()

    // Inserting new array elements and rows into 2nd section

    let ritualsArray = dataModel.lists[0].rituals
    var itemsArray = dataModel.lists[0].item
    itemsArray.appendContentsOf(ritualsArray)

    tableView.beginUpdates()

    var insertRitualsArray = [NSIndexPath]()

    for item in itemsArray {
        let itemIndex = itemsArray.indexOf(item)
        let indexPath = NSIndexPath(forRow: itemIndex!, inSection: 1)
        insertRitualsArray.append(indexPath)
    }

    tableView.insertRowsAtIndexPaths(insertRitualsArray, withRowAnimation: .Top)
    tableView.endUpdates()

}

已解决

此代码的问题与此线程的先前标题完全无关,这可能会误导与我有相同问题的人。因此,我会改变它。之前的标题(出于好​​奇)是:

“tableView.begin/end 更新不更新部分中的行数”

对于可能遇到此问题的其他人来说,问题不在 tableView 委托(delegate)中,也不在重新加载 tableview 数据中。为了可读性,我将 dataModel.list[0].item 放入 itemsArray 并将 dataModel.list[0].item 放入 ritualsArray。这显然在附加时更新了 itemsArray,而不是初始的 dataModel.list[0].item,这导致 tableView 中的第二部分不加载新的行数,从而在将行插入到不存在的行中时导致错误。

因此代替:

    let ritualsArray = dataModel.lists[0].rituals
    var itemsArray = dataModel.lists[0].item
    itemsArray.appendContentsOf(ritualsArray)

这解决了它:

dataModel.list[0].item += dataModel.list[0].rituals

希望它能帮助像我这样遇到这个问题的初学者。

最新更新

最近发现数组是值类型,不是引用类型。因此,将数组放入变量会生成该数组的副本,而不是用作原始数组的占位符。

初学者错误。

最佳答案

您收到的错误意味着数据源包含的项数与插入或删除行后的项数不同。这可能意味着数据没有插入到数据源数组中,或者数据不符合 numberOfRowsInSection 函数中 if 语句中的条件。要解决此问题,您应该在修改后记录数据源数组的内容以检查其内容。如果它们是您所期望的(即数据已正确添加),那么问题在于您评估其内容以确定行数的方式。如果内容不是您所期望的,那么问题出在您将数据插入数据源数组的方式上。

关于ios - Swift,将类数组添加到变量中不会更新原始类中的数组内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36808345/

相关文章:

c++ - 在C++/OpenCV中绘制频率直方图

ios - 加大节点的攻丝面积。 ios swift

ios - 如何以编程方式使用 swift 在 UITextField 下方显示 UITableView?

ios - bash ./Submodules/OTRKit/scripts/build-all.sh 配置 : error: C compiler cannot create executables

ios - 在 Objective-C 中获取当天的进度

arrays - 数组分割和提取

iphone - 在 UITableView 中对大量行/节进行动画处理性能不佳

arrays - 排序数组的问题

SwiftUI:ObservableObject 在重绘时不会保持其状态

ios - 在 UITableView 中水平滚动以查看 Xcode 6 和 Swift 中的长单元格