diff --git a/Sources/Plugins/AttachmentManager/AttachmentManager.swift b/Sources/Plugins/AttachmentManager/AttachmentManager.swift index 826e122a..a0ce919d 100755 --- a/Sources/Plugins/AttachmentManager/AttachmentManager.swift +++ b/Sources/Plugins/AttachmentManager/AttachmentManager.swift @@ -202,14 +202,21 @@ extension AttachmentManager: UICollectionViewDataSource, UICollectionViewDelegat final public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { - if let customSize = self.dataSource?.attachmentManager(self, sizeFor: self.attachments[indexPath.row], at: indexPath.row){ - return customSize - } - var height = attachmentView.intrinsicContentHeight if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { height -= (layout.sectionInset.bottom + layout.sectionInset.top + collectionView.contentInset.top + collectionView.contentInset.bottom) } + + // Prevent out of range error when the AttachmentCell has not been added the attachment array + if indexPath.row == attachments.count && showAddAttachmentCell { + return CGSize(width: height, height: height) + } + + let attachment = self.attachments[indexPath.row] + if let customSize = self.dataSource?.attachmentManager(self, sizeFor: attachment, at: indexPath.row){ + return customSize + } + return CGSize(width: height, height: height) }