添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

其中最主要的就是问题1, 有很多种情况可以引起,我们根据错误提示解释

plus or minus the number of sections inserted or deleted

1
Invalid update: invalid number of sections.  The number of sections contained in the collection view after the update (3) must be equal to the number of sections contained in the collection view before the update (4), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).

这个问题都是因为DataSource最终状态和执行insert、delete的预期结果不一致导致的,常见的原因如下

没有使用BathUpdate

如果同时改变Section和Item,则需要注意操作完毕 DataSource 后的方法,例如我们以下操作

  • 存在4个section,每个section有5个item
  • 删除第二个sect
  • 删除第三个section的第一个
  • 此时我们的的输入参数

    1
    2
    var sectSet = [1]
    var itemIndex = IndexPath(item: 0, section: 2)

    得到了入参数后,错误的方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    //此时DataSource还剩余 3个Section 
    //删除了 sect[1]
    //此时DataSouce和CollectionView对映
    self.collection.deleteSections(sect)
    //删除 IndexPath(item: 0, section: 2)
    //错误!!
    //此时的sect[2],是之前的第四个 sect[3](含有五个对象)
    //此时拥有4个对象的是 sect[1]
    self.collection.deleteItems(at: items)
    // DataSouce != CollectionView 无法对应
    //Crash

    应该使用正确的方法, 使用Batch(批量)进行更新

    1
    2
    3
    4
    self.collection.performBatchUpdates({
    self.collection.deleteSections(sect)
    self.collection.deleteItems(at: items)
    }, completion: nil)

    操作Section和Item后计算的Index不正确

    还有一种常见的错误是构造输入参数时的逻辑错误, 原因是操作DataSource时Index的动态变化

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    //准备结果容器
    var sectSet = IndexSet()
    var itemIndexes = [IndexPath]()
    //改变DataSource
    //!!错误
    sects.remove(at: 1)
    //删除了第二个sect后
    //此时整个sects共有3个元素,而不是之前的4个
    //此时的 sects[2] 指向了原本第四个 sect[3](含有五个对象)
    sects[2].remove(at: 0)
    //执行结束原本 sects[2](此时已经动态变化为sects[1])
    //并没有元素被删除
    //储存结果
    sectSet.insert(1)
    //此时构造的入参还是(2,0)
    itemIndex.append(IndexPath(item: 0, section: 2))
    //输出结果作为CollectionView的入参
    //构造错误导致入参错误
    complete(sectSet, itemIndex)
    //Crash

    Heap corruption detected

    1
    2
    3
    Heap corruption detected, free list is damaged at 0x600002115bc0
    *** Incorrect guard value: 3
    GateOfBabylon(16767,0x11a3c65c0) malloc: *** set a breakpoint in malloc_error_break to debug

    也是一种常见的DataSource和ViewCollection没有对齐的错误,常见于Insert操作

    如果同时Insert一组Sect和一个Item,恰巧Insert的Item是此前Insert的Sect,导致Sect总数没有变化 导致的

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    //准备结果容器
    var sectSet = IndexSet()
    var itemIndex = [IndexPath]()
    //插入一整个Sect
    //此时整个sects共有5个元素,而不是之前的4个
    sects.insert(["Insert", "Insert", "Insert"], at: 1)
    //在原本第二个Sect头部插入一个元素
    //此时 原本的sects[1] 变成了 sects[2]
    //当下的 sects[1] 为新插入的元素
    sects[1].insert("InsertItem", at: 0)
    //储存结果
    sectSet.insert(1)
    //此处就产生了构造错误, 因为 InsertItem 已经包含在sectSet里了
    itemIndex.append(IndexPath(item: 0, section: 1))
    //输入结果作为入参
    complete(sectSet, itemIndex)
    //Crash

    值得一提的是,如果 外部没有使用BatchUpdate(上文中错误的用法)才会产生这种Crash

    如果使用了 BatchUpdate 则会产生上一个Crash原因

    non-nil layout parameter

    1
    UICollectionView must be initialized with a non-nil layout parameter

    通常是使用了错误的初始化方法,正确的方式是

    1
    2
    3
    4
    5
    6
    7
    //根据layout初始化
    let layout = CustomLayout()
    let collection = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
    //初始化后更换layout
    let layout = CustomLayout()
    let collection = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
    collection.setCollectionViewLayout(layout, animated: false)
    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置: jsonContent: meta: false pages: false posts: title: true date: true path: true text: false raw: false content: false slug: false updated: false comments: false link: false permalink: false excerpt: false categories: false tags: true