我有这个代码 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) - Int{ return comments.count}func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) - UICol
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return comments.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = commentSection.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CommentCell return cell }
我有以下功能插入单元格
DispatchQueue.main.async{ let indexPath = IndexPath(row: self.comments.count, section: 0) self.commentSection.insertItems(at: [indexPath]) self.commentSection.reloadData() }
但每当我运行此代码时,它会打印此错误
libc++abi.dylib: terminating with uncaught exception of type NSException
并将我重定向到AppDelegate文件.即使错误写得非常清楚我也无法理解问题
在从collectionView插入或删除单元格之前,需要先修改数据源.self.collectionView?.performBatchUpdates({ let indexPath = IndexPath(row: self.comments.count, section: 0) comments.append(your_object) //add your object to data source first self.collectionView?.insertItems(at: [indexPath]) }, completion: nil)