1

Objective C でアプリを作成しています。コレクション ビューにカスタム セルがあります。最初に、SQLite Db からデータをロードし、各カスタム セル内のコレクション ビューに表示します。コレクションビューにセルを表示します。

コレクション ビューをリロードすると、コレクション ビューは黒色のままになります。

コード:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self readRowCount:@"SELECT COUNT(label) FROM colour;"];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%d", [arrColour count]);
    CustomViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ItemCell" forIndexPath:indexPath];

  //  if (cell == nil) {
    //    cell = [[CustomViewCell alloc]init];
    //}

    cell.label.text = [NSString stringWithFormat:@"%@",[ [arrColour objectAtIndex:indexPath.item ]label] ];

    CellData *redObj = [arrColour objectAtIndex:indexPath.row];
    CellData *greenObj = [arrColour objectAtIndex:indexPath.row];
    CellData *blueObj = [arrColour objectAtIndex:indexPath.row];

    float redF = [[redObj red] floatValue];
    float greenF = [[greenObj green] floatValue];
    float blueF = [[blueObj blue] floatValue];
    //NSLog(@"Colours: %f-%f-%f", redF, greenF, blueF);
    cell.backgroundColor = [UIColor colorWithRed:redF/255.0f green:greenF/255.0f blue:blueF/255.0f alpha:1];

    return cell;
}

データをリロードする場所 - ボタン:

- (IBAction)btnAdd:(id)sender {
   NSString *myRed = [NSString stringWithFormat: @"%1.4f", slideR.value];
   NSString *myGreen = [NSString stringWithFormat: @"%1.4f", slideG.value];
   NSString *myBlue = [NSString stringWithFormat: @"%1.4f", slideB.value];
    if(txtField != nil){


        [colMain reloadData];
    }

この状況の arrColour (可変配列) には、同じ量の要素が含まれます。コレクション ビューがリロードされていないのは何ですか?

4

2 に答える 2

1

Core Data を使用する代わりに Sqlite を直接使用している正当な理由はありますか?

とにかく、必要に応じて、現在のアプローチでそれを実現する方法は次のとおりです。

  1. NSArray*プロパティやインスタンス変数などのモデルに Sqlite データをロードします。配列を初期化して埋めます-viewDidLoad(または後で)。
  2. メソッドなどで、datasourceその配列を使用するように実装を変更します。return [self.colors]numberOfItemsInSection datasource
  3. モデル アイテムを追加または置換する場合は、モデル配列を変更 (または置換) します。-reloadData次に、コレクション ビューを呼び出します。

その他のヒント: - 本当にコレクション ビューに 3 つのセクション (項目の「グループ」) を表示したいですか? 実際にそうしなかった場合は、-numberOfSectionsInCollectionView実装を完全に削除してください。- すると、同じオブジェクトが 3 回取得されますarrColors[indexPath.item。きっとこれはあなたの意図ではありませんか?配列の要素として使用するか、カスタムサブクラスUIColorで色の値をラップすることを検討してください。NSObject

これが、開始する方法についてのいくつかの指針になることを願っています。

于 2013-02-03T19:08:13.140 に答える
0

実際、カスタム オブジェクトのプロパティを「強力」にする必要がありました。つまり、RGB カラーの値をカスタム オブジェクトに保存します。

于 2013-02-04T01:11:50.617 に答える