ios - 如何从 UICollectionView 中删除行?

标签 ios objective-c uicollectionview uicollectionviewcell nsindexpath

我的 UICollectionView 有两个 UICollectionResuabeView(自定义 View )和一个 UICollectionViewCell。在 UICollectionView 中形成单个实体。我将 Delete 按钮放在其中一个 UICollectionResuabeView 上,然后按下它想要删除该行(如果我说得对的话)。 这是 Storyboard的屏幕截图: enter image description here

我尝试通过多种方式获取它的一部分,但没有用。我想我只是对 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 的部分,删除我想要的部分。怎么做?)

enter image description here

最佳答案

您还需要从数组和 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/

相关文章:

ios - Error Domain=NSCocoaErrorDomain Code=3840 是属于服务器端还是本地代码端?

ios - UICollectionView 单行中的两个单元格

ios - 如何检测对 UICollectionView 中单元格的双击

ios - 做CABeginAnimation时如何正确设置模型

ios - 未调用 UIAlertView 委托(delegate)函数

iOS Safari 无法连接到安全的 websocket,但可以在桌面上运行

ios - 在 Obj-C 中使用 Swift 扩展

ios - 无法从Xcode 5导出未签名的IPA

ios - 如何知道在 uisearchbar 中连续打字以及在 ios 中停止打字

objective-c - Objective-C - UICollectionView 左侧/右侧的渐变渐变