【发布时间】: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