3

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])
        }
        }
        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Nivix
  • 171
  • 1
  • 1
  • 12
  • show some video or something how you need to peform when user scrolls – Saranjith Mar 28 '17 at 04:13
  • ok give me a second. – Nivix Mar 28 '17 at 04:13
  • may i know , why you want to delete last image? – Saurabh Prajapati Mar 28 '17 at 04:14
  • explain the thing you need to achive little bit more.. do you need to delete the cell when scrolling stops or ongoing scrolling? – Saranjith Mar 28 '17 at 04:22
  • umm i just delete the cell when the scrolling stops. Atleast what i am trying to do – Nivix Mar 28 '17 at 04:26
  • I like the detail you put into your question but it might be worthwhile to space things out better so your question will be a lot more readable to people who would like to answer your question but who are intimidated by how much text you have put into your question so perhaps a paragraph or two or three paragraphs may help make your question more friendly and acceptable for others to process and offer a solution for you. While we are on the subject of your code where does that code actually live? is it in a UITableViewDelegate method or is it somewhere else, like a view controller? – Michael Dautermann Mar 28 '17 at 04:28
  • ok ill edit it thanks Michael – Nivix Mar 28 '17 at 04:29
  • Micheal it is currently located in the viewcontroller with collectionview delegate and datasource implemented – Nivix Mar 28 '17 at 04:32
  • I think the issue your having is your basing your current image off of the scrollview offset, but your deleting images as you scroll screwing all that up. Thats why after you get to ` if currentImage > 1` you skip to 5. the equivalent of 2 images skipped – JustinM Mar 28 '17 at 04:43
  • hmm Justin so would be better to eliminate scrolling back? – Nivix Mar 28 '17 at 04:47
  • I'm sorry I can't give you specific code right now but you should look into the collectionView delegate method `didEndDisplaying cell` which will also give you the indexPath. Then you can remove it from there. – JustinM Mar 28 '17 at 04:52

2 Answers2

4

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)
}
handiansom
  • 783
  • 11
  • 27
  • would this be implemented under the scrollviewdidscroll that i currently have or separate? – Nivix Mar 28 '17 at 13:36
  • You can use it anywhere. Just pass the index of the item you want to delete. For example you wanted to delete item at row number 5 you just call that method this way --> deleteItemAtIndex(5) . @user7424546 – handiansom Mar 28 '17 at 13:47
  • for each item the user is on, the item in front of it (last viewed) will be deleted. Deleting an item at a certain index(5) doesn't help me because it only deletes the item before 6. – Nivix Mar 28 '17 at 15:23
  • i was not able to successfully use this to solve my problem – Nivix Mar 29 '17 at 01:39
  • I edited my answer pls check itout. it might help. @user7424546 – handiansom Mar 29 '17 at 01:43
  • currently: http://imgur.com/a/SwclQ and currently no actions are performed – Nivix Mar 29 '17 at 01:58
  • Thats not the way to call the delete method. Remove these lines from the delete method. currentIndex = 5 deleteItemAtIndex(currentIndex-1) – handiansom Mar 29 '17 at 02:04
  • Just call the delete method inside your scrollView delegate method. Which should look like this. func scrollViewDidScroll(_ scrollView: UIScrollView) { currentIndex = 5 deleteItemAtIndex(currentIndex-1) } @user7424546 – handiansom Mar 29 '17 at 02:05
  • ahh thats what i was wondering, i knew i needed to call it somewhere else, but could not tell – Nivix Mar 29 '17 at 02:06
  • added it to the scrollviewdidscroll and still nothing changed, is there something wrong in my image i posted, bc i did have to change some things from your answer – Nivix Mar 29 '17 at 02:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139315/discussion-between-handiansom-and-user7424546). – handiansom Mar 29 '17 at 02:53
  • Please check my updated. answer. it worked. I created the codes for you :) @user7424546 – handiansom Mar 29 '17 at 02:55
  • ok hold on i am going to post a video of what is happening for me. Its just staying on the first item, and when the user scrolls to next one, it goes back to the first item, and deletes the one they were just on for a second – Nivix Mar 29 '17 at 03:10
  • handiansorm i think that we are getting somewhere. – Nivix Mar 29 '17 at 20:26
  • i got it! i think, i changed the array to delete at indexpath - 1 – Nivix Mar 29 '17 at 20:29
0

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

Saranjith
  • 11,242
  • 5
  • 69
  • 122