我有一个像Pinterest一样定制的UICollectionView。我将此归档this Ray Wenderlich Tutorial。我在这里完成了一切,它完美无缺。尝试从集合视图中删除单元格时出现问题。我使用此函数进行删除:
extension PintViewController:FavouriteOffersDelegate{
func collectionViewCellDidTapHeart(_ sender: PntCollectionViewCell) {
guard let indexTapped = collectionView.indexPath(for: sender) else {
return
}
let id:Int = generalsOffersArray[indexTapped.row].id
print(indexTapped.item)
generalsOffersArray.remove(at: indexTapped.item)
collectionView.deleteItems(at: [indexTapped])
}
}
我理解问题必须在我使用的库中,但我是Swift的新手,并且无法清楚地了解我应该修改或移除的内容以使其工作。当执行此代码时,我得到的错误是控制台:
2017-10-18 17:25:20.637123 + 0200 RemoveItemsPinterest [1017:432532] *断言失败 - [UICollectionViewData validateLayoutInRect:],/ BuildRoot / Library / Cache / com.apple.xbs / Sources /的UIKit / UIKit的-3694.4.18 / UICollectionViewData.m:435 2017-10-18 17:25:20.638356 + 0200 RemoveItemsPinterest [1017:432532] * 由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:' UICollectionView接收到的布局属性索引路径不存在的单元格:{length = 2,path = 0 - 12}' ***第一次抛出调用堆栈: (0x1833bfd38 0x1828d4528 0x1833bfc0c 0x183d4ec24 0x18c82b834 0x18c82ab8c 0x18d2964ac 0x18d2412b8 0x18d241218 0x18d241148 0x18c82a1cc 0x18c7cd000 0x18739d0b4 0x1873a1194 0x18730ff24 0x187336340 0x18ca33744 0x18d112718 0x18d10b574 0x183368358 0x1833682d8 0x183367b60 0x183365738 0x1832862d8 0x185117f84 0x18c833880 0x1000c8ee4 0x182daa56c) libc ++ abi.dylib:以NSException类型的未捕获异常终止
布局对象的代码:
class PinterestLayout: UICollectionViewLayout {
var delegate:PinterestLayoutDelegate!
var numberOfColumns = 2
var cellPadding: CGFloat = 5.0
fileprivate var cache = [PinterestLayoutAttributes]()
fileprivate var contentHeight:CGFloat = 0.0
fileprivate var contentWidth: CGFloat {
let insets = collectionView!.contentInset
return collectionView!.bounds.width - (insets.left + insets.right)
}
override class var layoutAttributesClass : AnyClass {
return PinterestLayoutAttributes.self
}
override func prepare() {
if cache.isEmpty {
let columnWidth = contentWidth / CGFloat(numberOfColumns)
var xOffset = [CGFloat]()
for column in 0 ..< numberOfColumns {
xOffset.append(CGFloat(column) * columnWidth )
}
var column = 0
var yOffset = [CGFloat](repeating: 0, count: numberOfColumns)
for item in 0 ..< collectionView!.numberOfItems(inSection: 0) {
let indexPath = IndexPath(item: item, section: 0)
let width = columnWidth - cellPadding*2
let photoHeight = delegate.collectionView(collectionView!, heightForPhotoAtIndexPath: indexPath , withWidth:width)
let annotationHeight = delegate.collectionView(collectionView!, heightForAnnotationAtIndexPath: indexPath, withWidth: width)
let height = cellPadding + photoHeight + annotationHeight + cellPadding
let frame = CGRect(x: xOffset[column], y: yOffset[column], width: columnWidth, height: height)
let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)
let attributes = PinterestLayoutAttributes(forCellWith: indexPath)
attributes.photoHeight = photoHeight
attributes.frame = insetFrame
cache.append(attributes)
contentHeight = max(contentHeight, frame.maxY)
yOffset[column] = yOffset[column] + height
column = column >= (numberOfColumns - 1) ? 0 : column + 1
}
}
}
override var collectionViewContentSize : CGSize {
return CGSize(width: contentWidth, height: contentHeight)
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
// Loop through the cache and look for items in the rect
for attributes in cache {
if attributes.frame.intersects(rect ) {
layoutAttributes.append(attributes)
}
}
return layoutAttributes
}
}