我知道有很多问题已经提到了类似的问题。请不要低估或关闭问题。我刚刚尝试实施针对这些问题的解决方案。没有什么对我有用。我的情况也略有不同。在这里:
我的应用包含图片库。它是一个滚动视图,显示从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];
答案 0 :(得分:1)
确保您的[decksGallery viewWithTag:selectedEditDeck-1]
没有返回nil
,并且在刷新代码运行之前,图像实际上已从数据库中删除。
此外,您在创建代码中设置了imageView.tag = i;
,因为i
为0,这是每个UIView的标记默认值,您可以更好地修复你的创作代码也是如此。
答案 1 :(得分:0)
如果您只想从imageView中删除图像,还可以执行imageView.image = nil;
UIImageView*imageView = (UIImageView*)[decksGallery viewWithTag:selectedEditDeck-1];
[imageView removeFromSuperview];
imageView = nil;