collectionView
具体的には、 に関する興味深い問題がありdidSelectItemAtIndexPath
ます。
私のアプリには、SQL 経由でアクセスされる背景画像を持つ特定のビューが 1 つあります。そのビュー内には、collectionView を含むポップオーバーがあり、親ビューの背景を変更したり、購入可能な他の「セット」にアクセスしたりできます。
私の問題は、背景の変更にあります。すべてのアイテムがコレクション ビューに正しく表示され、親の背景が変更されます。問題は、選択されたアイテムが表示されるものではないことです. を介しNSLog
て、正しい画像が選択され、親に返されていることがわかります. 親には対応する がありNSLog
、ポップオーバーの と同じ画像 ID を示しますNSLog
。
の最初の項目をcollectionView
タップすると、親の背景が白に変わります。これは、利用可能な画像がないかのようです。2 番目のセルをタップすると、最初のセルの画像が表示されます。3 番目のセルをタップすると、2 番目のセルの画像が表示されます... というように続きます。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 データベースでした。0 ではなく 1 から始まりました。ID を変更すると、期待どおりに動作します。