【问题标题】:UICollection view not redrawing gradient buttonsUICollectionview 不重绘渐变按钮
【发布时间】:2012-11-17 18:46:26
【问题描述】:

我有一个 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

【问题讨论】:

  • 我尝试这段代码时没有问题,但是我直接在 viewDidLoad 中创建了我的鸟数组,而不是从数据库下载中创建,所以我认为这可能是你问题的根源。我认为您需要更新您的帖子以显示您从数据库中提取数据并将其放入鸟类的整个方法。

标签: ios xcode reloaddata uicollectionview collectionview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多