ios - CollectionView 默认项目选择

标签 ios objective-c uicollectionview uicollectionviewcell uicollectionviewdelegate

我已经实现了 单行多列 Collection View ,它位于 HomeViewController 的顶部。

最初,我想将一个默认项目设置为被选中并在下方用红色线条颜色突出显示,以及文本颜色为红色,请引用图像如下。

出于某种原因,它显示多个类别项目的红色线条颜色低于我的预期,但文本颜色是正确的。我不知道我做错了什么?

enter image description here

CategoryCollectionViewCell.m

@implementation CategoryCollectionViewCell
@synthesize categoryLabel,highlightedLabel;

- (void)setSelected:(BOOL)selected
{
    if(selected)
    {
        self.categoryLabel.textColor = [UIColor redColor];
        self.highlightedLabel.backgroundColor = [UIColor redColor];
    }
    else
    {
        self.categoryLabel.textColor = [UIColor lightGrayColor];
        self.highlightedLabel.backgroundColor = [UIColor clearColor];
    }
}
@end

HomeViewController.m

-(void) viewWillAppear: (BOOL) animated {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^(void) {
        [self loadFromURL]; // that is not related with categoryCollectionView
        dispatch_async(dispatch_get_main_queue(), ^{
                [self.categoryCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
            }
        });
}

最佳答案

您没有在此处发布所有相关代码。 从您的代码中可能有原因:

1) 您突出显示的标签颜色在​​单元格中设置为默认红色。当您传递代码时,只会更改该单元格。

为此,请在 xib/storyboard 中设置您突出显示的标签背景色。

关于ios - CollectionView 默认项目选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41845801/

相关文章:

ios - 如何在Xcode,Swift中拖放枚举?

objective-c - UICollectionView performBatchUpdates : animates all sections

ios - 是否可以在获取 header 之后、开始获取数据之前中断 NSURLSessionDownloadTask?

iphone - 带有标准 UI 的 iOS 上的 Twitter SDK

ios - 仅在一个 ViewController、 Objective-C 、iOS 中显示/隐藏状态栏

ios - 处理核心数据保存错误的正确方法

ios - UICollectionView 中的 viewForSupplementaryElementOfKind 返回 nil 并使用 swift 使应用程序崩溃

ios - 数组在函数中填充并显示但在 tableview swift 中不显示

objective-c - fmdb 插入日期给出 "invalid date"

objective-c - insertRowsAtIndexPaths 后 UITableViewCell 为零