2015-11-19 43 views
0

有它加载自定义collectionViewCell每7个细胞UICollectionView。这个问题是它从主数组的内容中消失了。我如何添加细胞,而不使用indexPath

(即如果mainArray.count == 17只15显示,因为indexPaths 具有为customCollectionViewCells被指定加载。)

我不希望用部分。我想知道这是否是一种加载这些单元而不会从主要内容中删除的方法?

我已经试过

func collectionView(_ collectionView: UICollectionView, 
    numberOfItemsInSection section: Int) -> Int { 
    let counter == 0; 
    if (index == 7 || index == 14) { 
     counter = customCellsArray.count 
    } 
    else{ 
     counter = mainArray.count 
    } 
    return counter; 
} 

回答

0

在您的数据源,返回你真正想要的细胞数量,包括自定义单元格。例如: -

func collectionView(_ collectionView: UICollectionView, 
    numberOfItemsInSection section: Int) -> Int { 

    return customCellsArray.count + mainArray.count; 
} 

然后,当你提供你的:

func collectionView(_ collectionView: UICollectionView, 
    cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    if (indexPath.item % 7 == 0 && indexPath.item > 0) { 
     // Return cell from customCellsArray. 
    } else { 
     // Return cell from mainCellsArray. 
    } 
} 

您必须将indexPath.item映射到你在两个数组需要适当的索引。

+0

'致命错误阵列索引超出range'(上mainArray)的每个的CollectionView到达最后信元时被抛出。 –

+0

好吧,你需要调试它。什么是指数?阵列的大小是多少?你如何计算指数?那里可能有一个错误。在计算它的地方设置一个断点,看看发生了什么。或者发布更多的代码,或者考虑用现在的代码来开启一个新的问题。 –