1

我有一个 UICollectionView,其中填充了单元格,每个单元格都有一个渐变按钮(MKgradientbutton https://github.com/mikekatz/iOS-UI-Utils/tree/master/Classes/Button%20Widgets)。通过按下其他 5 个按钮之一来“更改”视图,这反过来将按钮的数据加载到为 uicollectionview 提供数据源的数组中。其中一个参数是渐变按钮颜色。然而,在重新加载数据源并执行 [self.collectionView reloadData] 时,按钮标题和其他参数会发生变化,但颜色会经常变为完全错误的颜色。反复重新加载数据会解决这个问题——然而,更奇怪的是,按一下按钮似乎就能纠正按钮的颜色!在 reloadData 发生之前,我已经在行上对数据源数组进行了 NSLogged,并且所有颜色都是正确的。我在下面粘贴了一些代码 - 请耐心等待,因为该应用程序仍处于开发的早期阶段!

我真的看不出代码有任何问题——我相信 UICollectionView 需要“重绘”,但我认为 [self.CollectionView reloadData] 会解决这个问题!

将数据从 sqlite db 拉入对象然后添加到数组:

 Bird *bird = [[Bird alloc] init];

    bird.birdName = [results stringForColumn:@"name"];

    bird.buttonColour = [results stringForColumn:@"btncolour"];

[birds addObject:bird];

用于填充 UICollectionView 的数组“鸟”:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView2         cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView2     dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    Bird *bird = [birds objectAtIndex:(indexPath.row)];

    //Check button colour against index path of item
    NSLog(@"****INDEX PATH %i",indexPath.section*2 + indexPath.row);
    NSLog(@"****colour %@",bird.buttonColour);


    [cell.button setTitle:bird.birdName forState:UIControlStateNormal];
    [cell.button setTag:[birds indexOfObject:bird]];

    if ([bird.buttonColour isEqualToString:@"Green"]) {

 [cell.button setButtonColor:[UIColor greenColor]];

    }

    if ([bird.buttonColour isEqualToString:@"Orange"]) {

   [cell.button setButtonColor:[UIColor orangeColor]];
    }

    if ([bird.buttonColour isEqualToString:@"Red"]) {

      [cell.button setButtonColor:[UIColor redColor]];
    }

    if ([bird.buttonColour isEqualToString:@"Blue"]) {

   [cell.button setButtonColor:[UIColor blueColor]];
    }




    cell.button.titleLabel.numberOfLines = 0;
    cell.button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.button.titleLabel.textAlignment = NSTextAlignmentCenter;


      return cell; }

collectionViewCell.h(渐变按钮是单元格中的唯一项):

#import <UIKit/UIKit.h>
#import "MKGradientButton.h"

@interface CollectionViewCell : UICollectionViewCell


@property (nonatomic) IBOutlet MKGradientButton* button;


@end
4

1 回答 1

0

您可能必须通过 告诉按钮重绘自身[cell.button setNeedsDisplay]

于 2012-11-19T19:34:06.557 回答