从 UIScrollView 中删除 UIImageView

发布于 2024-11-30 12:28:28 字数 2353 浏览 1 评论 0原文

我的应用程序由一个图片库组成。它是一个滚动视图,显示从 sqlite 数据库检索的图片。用户可以滚动图像,添加或删除图像等。我可以动态地将图片添加到图库中。但当我需要实现删除功能时,问题就来了。我使用以下代码,但即使在调用 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];

My app consists of an image gallery. It is a scroll view which displays pics retrieved from the sqlite db. User can scroll the images, add or delete images etc. I can add a picture to the gallery dynamically. But the problem comes when I need to implement the delete functionality. I use the following code but even after calling removeFromSuperView the image is not getting removed from the scrollview.

-(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];
}

I have already created the uiscrollview in the loadview method. Then to refresh the view after every deletion and addition of images so that I can display the updated gallery, I use the following piece of code:

-(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 

//In the following fragment of code I add images to 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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远昼 2024-12-07 12:28:28

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

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

Make sure your [decksGallery viewWithTag:selectedEditDeck-1] is not returning nil and the image was actually deleted from your db before the refresh code running.

In addition, you are setting imageView.tag = i; in your creation code, since the i would be 0 which is the tag's default value for every UIView, you'd better fix your creation code as well.

穿越时光隧道 2024-12-07 12:28:28

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

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

If you just want to remove the image from your imageView, you can also do imageView.image = nil;

UIImageView*imageView = (UIImageView*)[decksGallery viewWithTag:selectedEditDeck-1];
[imageView removeFromSuperview];
imageView = nil;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文