我的 UICollectionView 有两个 UICollectionResuabeView(自定义 View )和一个 UICollectionViewCell。在 UICollectionView 中形成单个实体。我将 Delete 按钮放在其中一个 UICollectionResuabeView 上,然后按下它想要删除该行(如果我说得对的话)。 这是 Storyboard的屏幕截图:
我尝试通过多种方式获取它的一部分,但没有用。我想我只是对 UICollectionView 的工作原理了解不够。这是我试过的一些代码: 我知道我一团糟,但至少我在努力 ))
我试过这个来获取 indexPath,但是没有用。 -(IBAction)deletePressed:(UIButton* )发送者{
RecipeCollectionHeaderView *contentView = (RecipeCollectionHeaderView *)[sender superview];
RecipeViewCell *cell = (RecipeViewCell *)[contentView superview];
cell = (RecipeViewCell *)[contentView superview];
// determine indexpath for a specific cell in a uicollectionview
NSIndexPath *editPath = [self.collectionView indexPathForCell:cell];
NSInteger rowIndex = editPath.row;
NSInteger secIndex = editPath.section;
// NSIndexPath *indexPath = nil;
// indexPath = [self.collectionView indexPathForItemAtPoint:[self.collectionView convertPoint:sender.center fromView:sender.superview]];
// NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// NSSet *touches = [event allTouches];
//
// UITouch *touch = [touches anyObject];
//
// CGPoint currentTouchPosition = [touch locationInView:self.collectionView];
//
// NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint: currentTouchPosition];
NSLog(@"IndexPath CollectionView %ld", (long)rowIndex);
}
我也试过让它像这篇文章一样工作,但没有给出结果:how to access from UICollectionViewCell the indexPath of the Cell in UICollectionView 谢谢大家!
编辑! 我发现这很有效:
-(IBAction)deletePressed:(UIButton* )sender{
[self.ordersArray removeObjectAtIndex:0];
[self.collectionView reloadData];
}
我现在唯一需要做的就是通过按下的 UIBotton 获取 UICollectionView 的部分,删除我想要的部分。怎么做?)
最佳答案
您还需要从数组和 UICollectionView
中移除项目。
尝试使用此代码。
-(void) deleteRow:(NSInteger)index {
[self.contentView performBatchUpdates:^{
[arrayForRows removeObjectAtIndex:i];
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
[self.contentView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
} completion:^(BOOL finished) {
}];
}
对于单元格选择,请使用此代码段:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//Item position that you would like to delete.
indexPath.row;
}
关于ios - 如何从 UICollectionView 中删除行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28669019/