簡體   English   中英

如何更改特定UIViewCollectionCell的背景顏色?

[英]How can I change the background color of an specific UIViewCollectionCell?

我有一個包含三個部分的UIViewCollection,並且我想更改具有特定文本的UIViewCollectionCell的背景顏色。 為了獲得一些想法,這就是我想要的:

在此處輸入圖片說明

這是我在Objective-C上的代碼:

        BOOL isExactHour = [_working_dayArray containsObject:@"07:00"];

        if (isExactHour) {
            NSInteger indexValue = [_working_dayArray indexOfObject:@"07:00"];

            NSIndexPath *path = [NSIndexPath indexPathWithIndex:indexValue];

             UICollectionViewCell *cell = [_timetablesCollection cellForItemAtIndexPath:path];

            [cell setBackgroundColor:[UIColor colorWithRed:250 green:255 blue:150 alpha:.8]];

但是,如果我執行NSLog,則path返回<NSIndexPath: 0xc00000000000000e> {length = 1, path = 0} ,並且UIViewCollectionCell仍為白色。

有人可以幫忙嗎?

非常感謝。

您應該在像這樣的數據源方法期間更新單元格。 請記住,為了給所有確切在一個小時內到達的單元格着色,除了日期之類的字符串外,您可能還需要使用其他一些比較:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mycellidentifier" forIndexPath:indexPath];
    //Grab the string from whatever internal storage you are using. This would work if you only have one section, be aware it might not be exact for your case.
    NSString *hour = [_working_dayArray objectAtIndex:indexPath.row];
    if ([hour isEqualToString:@"7:00"]) {
      //Color cell background yellow
    } else {
      //Color cell background white
    }
  return cell;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM