开发者

Removing UIImageView from UIScrollView

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);
    gall开发者_如何学运维eryImage.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];


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜