0

我正在使用集合视图来删除数组。这就是我想要它做的事情:
第一步:用户点击垃圾桶
第二步:收藏视图被购买,用户只需点击即可删除图片。

然而奇怪的是,我的代码只删除了用户点击的第一张图片,并没有删除从第二张图片开始的另一张图片。我不知道为什么会这样,我这个删除过程的代码是

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"s:%d", [Trash count]);
    NSString *trashBin = [Trash objectAtIndex:indexPath.row];
    NSLog(@"k%@l",trashBin);
    [allImagesArray removeObjectAtIndex:indexPath.row];
    [self.collectionTrash reloadData];
    [self deleteMyFiles:trashBin];
}
NSString *myFileName;
-(void) deleteMyFiles:(NSString*)filePath {
    NSError *error;

    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
    }
}

随意询问更多代码

更新
是这个意思吗?

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"s:%d", [Trash count]);
    NSString *trashBin = [Trash objectAtIndex:indexPath.row];
    NSLog(@"k%@l",trashBin);
    [allImagesArray removeObjectAtIndex:indexPath.row];
    [self deleteMyFiles:trashBin];
    [collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]];
}
NSString *myFileName;

-(void) deleteMyFiles:(NSString*)filePath {
    NSError *error;

    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
    }
}
4

2 回答 2

0

尝试 [allImagesArray removeObject:trashBin]; 如果你想从数组中删除垃圾箱。

于 2013-10-25T07:11:46.153 回答
0

-deleteItemsAtIndexPaths您可以通过调用下面的UICollectionView类来删除选定的集合,而不是重新加载整个 collectionView

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"s:%d", [Trash count]);
NSString *trashBin = [Trash objectAtIndex:indexPath.row];
NSLog(@"k%@l",trashBin);
[allImagesArray removeObjectAtIndex:indexPath.row];
[self deleteMyFiles:trashBin];
[collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]];

}

于 2013-10-25T07:33:49.137 回答