如何在ios的其他函数中获取收集单元格?
我有UICollectionView,其中包含许多自定义单元格。当用户长按时,我有一个长按手势,然后单元格开始摇晃,并在其上添加了删除按钮。当我按删除按钮时,单元格将从集合视图中删除 长按代码。如何在ios的其他函数中获取收集单元格?,ios,objective-c,iphone,ipad,Ios,Objective C,Iphone,Ipad,我有UICollectionView,其中包含许多自定义单元格。当用户长按时,我有一个长按手势,然后单元格开始摇晃,并在其上添加了删除按钮。当我按删除按钮时,单元格将从集合视图中删除 长按代码。 -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { p = [gestureRecognizer locationInView:self.collection_view]; NSIndexP
-(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;
}
}
这里我刚刚添加了
[\u deleteButton removeFromSuperview]代码>如何在每个函数中获取单元格&删除删除按钮。要根据单元格的索引XPath检索单元格,可以调用
UICollectionViewCell *someCell = [myCollectionView cellForItemAtIndexPath:indexPath];
然后用鼠标删除删除按钮
if (someCell) {
// Remove the delete button on the cell.
[someCell.deleteButton removeFromSuperView];
}
有两种方法
将IndexPath存储在全局变量中
在你的
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];
}