2
votes

I have a horizontally scrollable UICollectionView, I am setting the horizontal direction using UICollectionViewFlowLayout.

When I delete one of the cell using -

[self.collectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths];

I notice that on scrolling back to the first UICollectionViewCell(Which is not visible when the above cell is deleted) overlaps with the second UICollectionViewCell as shown below

enter image description here

Sometimes instead of overlapping it ends up like -

enter image description here

My cellForItemAtIndexPath looks like -

ABCollectionViewCell *cell = (ABCollectionViewCell *)[cv dequeueReusableCellWithReuseIdentifier:@"abCell" forIndexPath:indexPath];

    cell.name.text = [self.categoryArray objectAtIndex:indexPath.row];
    cell.contactButton.tag = indexPath.row;

    cell.layer.cornerRadius = 5.0;
    cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
    cell.layer.borderWidth = 1.0;
    cell.clipsToBounds = YES;

    return cell;

I have tried all the similar issues solution online but none of them seem to be working. Any help would be appreciated.

UICollectionViewFlowLayout -

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
flowLayout.estimatedItemSize = CGSizeMake(102.0, 102.0);
flowLayout.minimumLineSpacing = 6.0f;
flowLayout.minimumInteritemSpacing = 2.0f;
[self.collectionView setCollectionViewLayout:flowLayout];
2

2 Answers

0
votes

Perform delete like this

[self.collectionView performBatchUpdates:^{
    [self.collectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths]; 
} completion:^{
    [self.collectionView layoutIfNeeded];
}];
0
votes

Use this function

func collectionView(_ collectionView: UICollectionView,
                      layout collectionViewLayout: UICollectionViewLayout,
      insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
}