【问题标题】:Removing UIImageView from UIScrollView从 UIScrollView 中删除 UIImageView
【发布时间】:2023-03-27 12:41:01
【问题描述】:

我知道已经有很多类似的问题被问到了。请不要投票或关闭问题。我刚刚尝试实施针对这些问题给出的解决方案。没有什么对我有用。我的情况也略有不同。就是这样:

我的应用包含一个图片库。这是一个滚动视图,显示从 sqlite db 检索到的图片。用户可以滚动图片、添加或删除图片等。我可以动态地将图片添加到图库中。但是当我需要实现删除功能时,问题就来了。我使用以下代码,但即使在调用removeFromSuperView 之后,图像也没有从滚动视图中删除。

-(void)deleteDeck{ 

if(selectedEditDeck!=0){
    [deck deleteSelectedDeck:selectedEditDeck]; //deleting from database

   //problem starts here ***
    [(UIImageView*)[decksGallery viewWithTag:selectedEditDeck-1]removeFromSuperview];
    [self loadGallery];
    selectedEditDeck=0;
  //Ends here*****

    [tableData release];
    tableData=[NSMutableArray array];
    [self showCardNamesinTable];
    [aTableView reloadData];
}

我已经在 loadview 方法中创建了 uiscrollview。然后在每次删除和添加图像后刷新视图以便显示更新后的图库,我使用以下代码:

-(void)loadGallery{                                                                                //Reloading all for adding a single deck image.

//Database part****
NSString *sqlStr = [NSString stringWithFormat:@"select first_card from decksTable"]; 
char *sql = (char*)[sqlStr UTF8String];
kidsFlashCardAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSMutableArray *galleryImagesArray=[appDelegate.dbConnection fetchColumnFromTable:sql col:0];
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *docDirectory = [sysPaths objectAtIndex:0];
numberofImages = [galleryImagesArray count];
printf("number is %d",numberofImages);//Database part of code ends here 

//在下面的代码片段中,我将图像添加到 UIscrollView

    for (int i = 0; i < [galleryImagesArray count]; i++) {
    CGFloat yOrigin = i * 65;
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", docDirectory,[galleryImagesArray objectAtIndex:i]];
    galleryImage = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin+140, 15,50,50 )];
    //galleryImage.tag=[galleryImagesArray count];
    galleryImage.tag=i;
    printf("THE TAG IS %d",galleryImage.tag);
    galleryImage.clipsToBounds=YES;
    galleryImage.layer.cornerRadius=11.0;
    galleryImage.backgroundColor=[UIColor clearColor];
    galleryImage.image =[UIImage imageWithContentsOfFile:filePath];
    [decksGallery addSubview:galleryImage];
    [galleryImage release];
}
decksGallery.contentSize = CGSizeMake(115*[galleryImagesArray count], 80);
//[decksGallery reloadInputViews];

【问题讨论】:

  • 抱歉代码的格式化方式...顺便说一句,我在删除时使用 imageview 的标签来引用它们...
  • 您能否设置一个断点并验证您的 selectedDeck 是否已设置以及用于从数据库中删除卡片组的代码是否正在执行。删除后检查数据库中的新负载是否再次提供相同的图像。
  • 不,我将图像路径存储在数据库中并检索它.....实际上,当我按顺序删除它时,图像会被删除......假设我有五个图像......当我开始从图像中删除时带有标签 5...它被删除了....但是当我随机执行它时它不会被删除..n 当我选择图像 wid 标签零时还有一件事..tat 是第一个 img..所有图像被删除...

标签: iphone ios uiscrollview uiimageview


【解决方案1】:

确保您的[decksGallery viewWithTag:selectedEditDeck-1] 没有返回nil,并且在刷新代码运行之前,图像实际上已从您的数据库中删除。

此外,您在创建代码中设置了imageView.tag = i;,因为i 将是0,这是每个UIView 标签的默认值,您最好也修复您的创建代码。

【讨论】:

  • 不,它没有返回零。关于你所说的关于标签的默认值,你能解释一下吗??
  • 你最好这样设置.tag:imageView.tag = kImageViewStartAt + i; ,其中 kImageViewStartAt 被#定义为一个大数,例如 1000。你可以使用 [*** viewWithTag:kImageViewStartAt + selectedDeck - 1];
  • 关于你的问题本身,我想也许你应该检查你得到的带有标签的 imageView 是否是你想要删除的视图。此外,您最好使用 NSLog("SubView Count: %@", [[decksGallery subViews] count]); 检查您的 decksGallery 有多少 imageViews。看看计数是否正确。
  • 非常感谢....我检查了这个..NSLog("SubView Count: %@", [[decksGallery subViews] count]);子视图的数量比我预期的要多。我一直在再次调用 for 循环 n 再次删除子视图...再次非常感谢您的提示...它就像一个魅力:D :)
【解决方案2】:

如果你只是想从你的 imageView 中删除图像,你也可以这样做 imageView.image = nil;

UIImageView*imageView = (UIImageView*)[decksGallery viewWithTag:selectedEditDeck-1];
[imageView removeFromSuperview];
imageView = nil;

【讨论】:

  • 你能在断点命中 [imageView removeFromSuperview] 时检查 imageView 对象中的内容吗?行...