ios - Swift 3 部分中的行数无效

标签 ios swift uitableview swift3 reloaddata

我有一个包含 Card() 类的数组字典,我有一个调用移动一个数组的函数的按钮

    static var DeliveryStatusArray =
    [
        "claimable": [Card](),
        "onTime": [Card](),
        "future": [Card](),
        "claimDone": [Card](),
        "tooOld": [Card](),
    ]

我移动卡的功能是

    static func moveCard(card:Card) -> Void {
    var pos:Int = -1
    var index:Int = 0
    while(index < (DashboardManager.DeliveryStatusArray["claimable"]?.count)!)
    {
        if (DashboardManager.DeliveryStatusArray["claimable"]?[index].idCard == card.idCard)
        {
            pos = index
        }
        index += 1
    }
    if (pos > -1)
    {
        let card:Card = (DashboardManager.DeliveryStatusArray["claimable"]?[pos])!
        DashboardManager.DeliveryStatusArray["claimable"]?.remove(at: pos)
        DashboardManager.DeliveryStatusArray["claimDone"]?.append(card)
    }
}

完成后,我会向我的 View 发布一条通知以调用此函数

    func notificationFinish(notification:Notification) -> Void{
        let sectionClaimable:Int = (api.dictionary["delivery"]?.index(of: "claimable"))! // Is 1
        let sectionClaiDone: Int = (api.dictionary["delivery"]?.index(of: "claimDone"))! // Is 4
        tableView.reloadSections(IndexSet(integer: sectionClaimable), with: .top)
        tableView.reloadSections(IndexSet(integer: sectionClaimDone), with: .top)
        return
    }

第一个循环我在 * DeliveryStatusArray["claimDone]* 中得到了 2 张卡,在调用 moveCard() 后我得到了三张卡

我收到错误

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 4. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (2), 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).'

重新加载数据时无法获取更多项目?

最佳答案

如果我理解正确的话,你的代码比它需要的要复杂得多......

// Get the index of card in claimable with matching idCard
guard let index = deliveryStatusArray["claimable"]?.index(where: {$0.idCard == card.idCard }) else { return }

// Remove the card at that index
let card = deliveryStatusArray["claimable"]?.remove(at: index)

// Append the card
deliveryStatusArray["claimDone"]?.append(card)

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

相关文章:

ios - 读取本地化字符串

ios - 在 Swift 中使用 xib 创建 UIView

iphone - 从 URL iphone sdk 将图像加载到 tableView

ios - 如何为不同的屏幕尺寸设置不同的自动布局约束

ios - cellForItem(位于 : IndexPath) method doesn't call with UICollectionViewCell subclass

ios - 动态自动布局

swift - 属性 setter 中的 NilLiteralConvertible

iphone - UITableViewCell 附件 Checkmarx 问题,许多单元格仅通过检查一行来检查

ios - 带有自动布局的 UIScrollView 失败,并在启动时消失

ios - UITableViewCell 分隔线错位