Nivix
Nivix

Reputation: 181

Deleting Items out of collection view

I have currently created an array of images [1 - 8]. I have also created a collectionview that pulls images out of the imageArray and puts images in the imageview which is in the cell. Please note that the cell has one imageView in it and it takes up the whole screen, horizontal scrolling and paging are both enabled.

Right now with the current code I have, is very odd what is currently happening so I will tell you what is currently going on. So what currently is happening (I'm going by image 1 which index 0) if the image is on 2 (index 1) and then you swipe next to 3 (index 2), it skips image 3 (index 2) and 4 (index 3) and sits on image 5 (index 4), so when I mean skips, I mean it slides past the image you just swiped to and one more.

(Oddly it deletes image 1 and 2) once on 5. I believe this is due to the it is updating the index or setting it to 2 over again because it just deleted 0. I know this might be hard to get but just think of scrollview with paging and when you swipe to the third image, it skips the one your on and one more and slides you to image 5 where it stays place.

So far thanks to some of you, I have came up with this code below, but I am hoping someone will be able to solve this awful mess.

 var currentImage = 0
   func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let x = floor(myCollectionView.contentOffset.x / view.frame.width)
    if Int(x) != currentImage {
        currentImage = Int(x)
    }
    if currentImage > 1 {
    for collectionCell in myCollectionView.visibleCells  as [UICollectionViewCell]  {
        let visibleCells = myCollectionView.visibleCells
        if visibleCells.first != nil {
            if let indexPath = myCollectionView.indexPath(for: collectionCell as UICollectionViewCell) {
                let indexPathOfLastItem = (indexPath.item) - 1
                let indexPathOfItemToDelete = IndexPath(item: (indexPathOfLastItem), section: 0)
                imageArray.remove(at: (indexPath.item) - 1)
                myCollectionView.deleteItems(at: [indexPathOfItemToDelete])
        }
        }
        }
    }
}

Upvotes: 3

Views: 2605

Answers (2)

handiansom
handiansom

Reputation: 783

These codes will delete a specific item using the item index. This is working great!

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    var visibleRect = CGRect()

    visibleRect.origin = myCollectionView.contentOffset
    visibleRect.size = myCollectionView.bounds.size

    let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
    let visibleIndexPath: IndexPath = myCollectionView.indexPathForItem(at: visiblePoint)!

    print(visibleIndexPath)

    fruitArray.remove(at: visibleIndexPath.row )
    myCollectionView.deleteItems(at: [visibleIndexPath])
    myCollectionView.scrollToItem(at: IndexPath.init(row: visibleIndexPath.row-1, section: 0), at: UICollectionViewScrollPosition.centeredHorizontally, animated: false)
}

Upvotes: 4

Saranjith
Saranjith

Reputation: 11577

Use scrollViewDidEndDragging to detect when user stops scrolling and delete the cell,..

Objective C

 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
        // if decelerating, let scrollViewDidEndDecelerating: handle it
        if (decelerate == NO) {

            [self deleteCell];
        }
    }

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        [self deleteCell];
    }

    - (void)deleteCell {

        NSIndexPath *pathForCenterCell = [self.tableView indexPathForRowAtPoint:CGPointMake(CGRectGetMidX(self.tableView.bounds), CGRectGetMidY(self.tableView.bounds))]; // if you want middle cell

        NSIndexPath *firstVisibleIndexPath = [[self.tableView indexPathsForVisibleRows] objectAtIndex:0]; // if you want first visible cell

        NSIndexPath *lastVisibleIndexPath = [[self.tableView indexPathsForVisibleRows] objectAtIndex:[self.tableView indexPathsForVisibleRows].count];   // last cell in visible cells



        myCollectionView.beginUpdates() //if you are performing more than one operation use this 
        yourImage.removeObjectAtIndex(myNSIndexPath.row)
            myCollectionView.deleteItems(at: [lastVisibleIndexPath])
        myCollectionView.endUpdates() 


     }

Swift 3.0

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    // if decelerating, let scrollViewDidEndDecelerating: handle it
    if decelerate == false {
        deleteCell()
    }
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    deleteCell()
}

func deleteCell() {
    var pathForCenterCell: IndexPath? = tableView.indexPathForRow(at: CGPoint(x: CGFloat(tableView.bounds.midX), y: CGFloat(tableView.bounds.midY)))
        // if you want middle cell
    var firstVisibleIndexPath: IndexPath? = (tableView.indexPathsForVisibleRows?[0] as? IndexPath)
        // if you want first visible cell
    var lastVisibleIndexPath: IndexPath? = (tableView.indexPathsForVisibleRows?[tableView.indexPathsForVisibleRows?.count] as? IndexPath)
    // last cell in visible cells
    myCollectionView.beginUpdates()()
    //if you are performing more than one operation use this 
    yourImage.removeObjectAtIndex(myNSIndexPath.row)
    myCollectionView.deleteItems
}

Happy coding

Upvotes: 0

Related Questions