1

我正在用 Objective C 编写一个应用程序。我在集合视图中有自定义单元格。最初,它从 SQLite Db 加载数据并将其显示在每个自定义单元格内的 Collection 视图中。它在集合视图上显示单元格。

一旦我重新加载集合视图,集合视图将保持黑色。

代码:

-(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

您是否有充分的理由直接使用 Sqlite 而不是使用 Core Data,它很好地抽象了大部分低级持久性复杂性?

无论如何,如果您必须这样做,您可以按照以下方法使用当前方法:

  1. 将您的 Sqlite 数据加载到模型中,例如NSArray*属性或实例变量。初始化并填充数组-viewDidLoad(或以后)。
  2. 更改您的datasource实现以使用该数组,例如return [self.colors]numberOfItemsInSection datasource方法中。
  3. 添加或替换模型项时更改(或替换)模型数组。然后调用-reloadData集合视图。

其他一些提示: - 您确定要在集合视图中有 3 个部分(“组”项)吗?如果您实际上没有,请-numberOfSectionsInCollectionView完全删除您的实现。- 当你这样做时,你会得到相同的对象三次arrColors[indexPath.item。这肯定不是你的本意?考虑UIColor用作数组的元素,或将颜色值包装在自定义NSObject子类中。

希望这能给你一些关于如何开始的指导。

于 2013-02-03T19:08:13.140 回答
0

事实上,我需要做的是为我的自定义对象“强”创建属性。那就是我将 RGB 颜色的值存储在自定义对象中。

于 2013-02-04T01:11:50.617 回答