如何在ios的其他功能中获取集合单元格?
Posted
技术标签:
【中文标题】如何在ios的其他功能中获取集合单元格?【英文标题】:How to get the collection cell in other function in ios? 【发布时间】:2015-11-14 07:58:55 【问题描述】:我有 UICollectionView,其中包含这么多自定义单元格。当用户长按然后单元格开始晃动并在其上添加删除按钮时,我有一个长按手势。当我按下删除按钮时,单元格将从集合中删除查看。
长按代码。
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
p = [gestureRecognizer locationInView:self.collection_view];
NSIndexPath *indexPath = [self.collection_view indexPathForItemAtPoint:p];
if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
return;
if (indexPath == nil)
NSLog(@"couldn't find index path");
else
[[NSUserDefaults standardUserDefaults]setValue:@"yes" forKey:@"longPressed"];
[self.collection_view reloadData];
if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
NSLog(@"UIGestureRecognizerStateEnded");
//Do Whatever You want on End of Gesture
else if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
NSLog(@"UIGestureRecognizerStateBegan.");
//Do Whatever You want on Began of Gesture
pgr
= [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(handePanPress:)];
// To detect after how many seconds you want shake the cells
pgr.delegate = self;
[self.collection_view addGestureRecognizer:pgr];
//show the done button here
navButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(navBtnDone:)];
self.navigationItem.rightBarButtonItem = navButtonDone;
当长手势开始时,我还会在按下导航栏按钮时在导航栏上添加右键,我会停止动画并删除删除按钮。我可以删除 iPhone 5s 中的删除按钮,但不能在 iPhone 6 中删除。
下面是代码
- (IBAction)navBtnDone:(id)sender
if([[[NSUserDefaults standardUserDefaults]valueForKey:@"longPressed"] isEqualToString:@"yes"])
[[NSUserDefaults standardUserDefaults]setValue:@"no" forKey:@"longPressed"];
[_deleteButton removeFromSuperview];
[self.collection_view reloadData];
[self.collection_view removeGestureRecognizer:pgr];
self.navigationItem.rightBarButtonItem=nil;
这里我刚刚添加了[_deleteButton removeFromSuperview];
如何在每个函数中获取单元格并删除删除按钮。
【问题讨论】:
【参考方案1】:要根据索引路径检索单元格,您可以调用
UICollectionViewCell *someCell = [myCollectionView cellForItemAtIndexPath:indexPath];
然后删除删除按钮
if (someCell)
// Remove the delete button on the cell.
[someCell.deleteButton removeFromSuperView];
【讨论】:
【参考方案2】:有两种方法。
-
将 IndexPath 存储在全局变量中。
在您的 .h 中
NSIndexPath *globalIndexPath
在您的手势识别器中
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
p = [gestureRecognizer locationInView:self.collection_view];
globalIndexPath = [self.collection_view indexPathForItemAtPoint:p];
//Other Stuff
在您的删除按钮操作中
- (IBAction)navBtnDone:(id)sender
//Other Stuff
UICollectionViewCell *cell = [myCollectionView cellForItemAtIndexPath: globalIndexPath];
if (cell)
[cell.deleteButton removeFromSuperView];
将 Indexpath.row 值设置为 Barbutton 的标记值
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
p = [gestureRecognizer locationInView:self.collection_view];
NSIndexPath *indexPath = [self.collection_view indexPathForItemAtPoint:p];
//Other Stuff
//show the done button here
navButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(navBtnDone:)];
navButton.tag = indexPath.row //IMPORTANT
self.navigationItem.rightBarButtonItem = navButtonDone;
在您的按钮操作中
- (IBAction)navBtnDone:(id)sender
UIBarButtonItem *btn = (UIBarButtonItem *)sender;
UItableViewCell *cell = [myCollectionView cellForItemAtIndexPath: [NSIndexPath indexPathWithRow: btn.tag inSection:0]];
[cell.deleteButton removeFromSuperView];
【讨论】:
以上是关于如何在ios的其他功能中获取集合单元格?的主要内容,如果未能解决你的问题,请参考以下文章
在集合视图中显示两个不同的单元格 - Swift 2.0 iOS