2

我有一个有趣的问题collectionView,特别是didSelectItemAtIndexPath

我的应用程序有一个带有背景图像的特定视图,可通过 SQL 访问。在该视图中是一个带有 collectionView 的弹出框,用于更改父视图的背景,并访问其他可供购买的“集合”。

我的问题是背景的变化。所有项目都在集合视图中正确显示,并且父级的背景会发生应有的变化。问题是选择的项目不是显示的项目,即使通过NSLog,我可以看到正在选择正确的图像并将其传递回父项。父级中有一个对应NSLog的,它显示与弹出框相同的图像 id NSLog

中的第一项collectionView,当点击时,会将父项的背景更改为白色,就好像没有可用的图像一样。点击第二个单元格将显示第一个单元格的图像。点击第三个单元格将显示第二个单元格的图像......等等。有 15 张图片可供选择。

这是didSelect方法:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_currentView isEqualToString:@"New_Journal"])
    {
        MyAppDelegate *appDelegate = (MyAppDelegate *)[UIApplication.sharedApplication delegate];
        // to get the variable back to the parent
        appDelegate.loadBackgroundID = indexPath.row; 
        // interact with the SQL via _settingsDao
        [_settingsDao updateJournalBackgroundId:_journalId withBackgroundId:appDelegate.loadBackgroundID];
        NSLog(@"Selected background: %d", indexPath.row);
        // notification to let EntryView know the background has changed
        [NSNotificationCenter.defaultCenter postNotificationName:@"aBackgroundChanged" object:self];
    }
}

编辑以显示 SQL 交互方法:

-(void)updateJournalBackgroundId:(int)journalID withBackgroundId:(int)newBackgroundId
{
    NSString *query = @"UPDATE BgIconBorderSettings SET background_id = ? WHERE journal_id = ?";
    FMDatabase *db = [dbUtils sharedDB];
    [db executeUpdate:query,[NSNumber numberWithInt:newBackgroundId],[NSNumber numberWithInt:journalID]];
    if([db hadError])
    {
        NSLog(@"Error %d: %@",[db lastErrorCode],[db lastErrorMessage]);    
    }       
}

任何人都对如何显示被点击单元格的正确图像有任何想法?感谢您为解决此问题提供的任何帮助。

正如 jhilgert 指出的那样……它是 SQL 数据库。它从 1 而不是 0 开始。更改了 ID,它按预期工作。

4

0 回答 0