InsidedCollectionView
示例
要运行示例项目,首先克隆仓库,然后从示例目录运行pod install
要求
安装
InsidedCollectionView可通过CocoaPods获取。要安装它,只需将以下行添加到Podfile中
pod 'InsidedCollectionView'
用法
只需导入模块,实例化一个带有您想要的范围的Manager,并使用Manager的方法来获取合适的索引而不是计算出的索引
import InsidedCollectionView
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
let insideCollectionViewManager = InsidedCollectionViewManager(range: 3)
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return insideCollectionViewManager.numberOfElementCountingInserted(inCount: itemsCount)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "reuseIdentifier", for: indexPath)
if insideCollectionViewManager.isInserted(indexPath: indexPath) {
let adIndexPath = insideCollectionViewManager.insertedIndex(indexPath: indexPath)
cell.configureAd(adNumber: adIndexPath.row)
} else {
let myIndexPath = insideCollectionViewManager.trueIndexPath(indexPath: indexPath)
let item = items[myIndexPath.row]
cell.configure(item)
}
return cell
}
}
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if insideCollectionViewManager.isInserted(indexPath: indexPath) {
print("selected inserted item at", insideCollectionViewManager.insertedIndex(indexPath: indexPath).row)
} else {
let trueIndex = insideCollectionViewManager.trueIndexPath(indexPath)
print("selected item at", trueIndex.row)
}
}
}
您可以在运行时更改范围,但请记住在您的collectionView上调用reloadData
!
insideCollectionViewManager.range = 14
collectionView.reloadData()
如果想要插入/删除/移动项目,请使用InsidedCollectionViewManager
的相应方法
open func insertItems(at indexPaths: [IndexPath], beforeCount: Int, inCollectionView: UICollectionView)
open func deleteItems(at indexPaths: [IndexPath], beforeCount: Int, inCollectionView: UICollectionView)
open func reloadItems(at indexPaths: [IndexPath], inCollectionView: UICollectionView)
open func moveItem(at indexPath: IndexPath, to newIndexPath: IndexPath, inCollectionView: UICollectionView)
对于插入和删除操作,您必须传递在您的插入或删除操作之前此处的项目数量(不包括已插入的项目)。这样,管理器就可以计算所需的插入单元格数量,相应地插入并添加它们,并将您的新的项目添加到它们正确的新索引处。如有需要,请参阅示例。
作者
Philippe Auriach
许可证
InsidedCollectionView 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。