2017-03-28 49 views
2

我目前已经创建了一个图像数组[1 - 8]。我还创建了一个collectionview,用于从imageArray中提取图像,并将图像放入单元格中的imageview。请注意,单元格中有一个imageView,它占用了整个屏幕,水平滚动和分页都启用。删除项目收集视图

现在使用我现在的代码,很奇怪现在发生了什么,所以我会告诉你现在正在发生什么。因此,如果图像是2(索引1),然后你滑过3(索引2),它会跳过图像3(索引2)和4(索引3),并坐在图像5(索引4)上,所以当我的意思是跳过时,我的意思是它滑过你刚刚滑过的图像,还有一个。

(奇怪它会删除图像1和2)一次5.我认为这是由于它正在更新索引或将其设置为2,因为它只是删除0.我知道这可能很难得到但只要想起分页的滚动视图,当你滑动到第三个图像时,它会跳过你的一个图像,并将其滑动到图像5的位置。

到目前为止,由于你们中的一些人,我已经提出了下面的代码,但我希望有人能够解决这个可怕的混乱。

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]) 
     } 
     } 
     } 
    } 
} 
+0

显示一些视频或其他东西当用户滚动时你需要做什么 – Saranjith

+0

ok给我一秒钟。 – Nivix

+0

我可以知道,为什么要删除最后一张图片? –

回答

1

这些代码将使用项目索引删除特定项目。这非常棒!

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) 
} 
+0

这将在我目前拥有或分开的scrollviewdidscroll下实现吗? – Nivix

+0

你可以在任何地方使用它。只需传递要删除的项目的索引即可。例如,您想要删除第5行的项目,您只需以此方式调用该方法 - > deleteItemAtIndex(5)。 @ user7424546 – handiansom

+0

对于用户所在的每个项目,其前面的项目(上次查看)将被删除。删除一个特定索引(5)的项目不会帮助我,因为它只会删除6之前的项目。 – Nivix

0

使用scrollViewDidEndDragging到当用户停止滚动检测和删除单元,..

目标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() 


    } 

夫特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 
} 

快乐Ç oding

+0

它会很好,如果它在迅速,我可以尝试转换它。 – Nivix

+0

也加入了迅捷版本。 – Saranjith

+0

好吧,我需要scrollviewdidscroll代码吗? – Nivix

相关问题