iphone - UICollectionView 0th 索引改变完整 View 的颜色

标签 iphone ios objective-c uicollectionview uicollectionviewcell

我正在使用 UICollectionViewController 以橙色背景显示日期,当用户单击该天时,该天的背景应更改为灰色。该程序每天都运行良好,除了星期一,其中索引 path.row 值为 0(它是该系列中的第一项)。它将所有 View 的背景(包括其本身)更改为橙色

    @implementation DayControllerViewController
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
    return self;}

    -(void)viewDidLoad{
    [super viewDidLoad];
    dayLabels =  [NSArray arrayWithObjects:@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday", nil];
    //[self.collectionView reloadData];    
}

    -(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return dayLabels.count;
}

    UICollectionViewCell *cell;

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UILabel *recipeImageView = (UILabel *)[cell viewWithTag:100];
    [recipeImageView setText:  [dayLabels objectAtIndex:indexPath.row]];
    [recipeImageView setTag : indexPath.row];

    UIColor *orangecol = [UIColor colorWithRed:236.0/255 green:85.0/255 blue:50.0/255 alpha:1];

    [recipeImageView setBackgroundColor:orangecol];
    NSLog(@"Label Tag: %i",recipeImageView.tag);
    return cell;

}

    -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"this is caled");
    return YES;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIColor *orangecol = [UIColor colorWithRed:236.0/255 green:85.0/255 blue:50.0/255 alpha:1];

    UIColor *greycol = [UIColor colorWithWhite:0 alpha:0.3];

    NSLog(@"DidSelect Called");
    UILabel* lbl = (UILabel*)[collectionView viewWithTag:indexPath.row];
    NSLog(@"Label With Tag: %i",lbl.tag);
    if([lbl.backgroundColor isEqual: orangecol]){
        NSLog(@"lbl Clicked %@",lbl.text);
        [lbl setBackgroundColor:greycol];
    }
    else {
        [lbl setBackgroundColor:orangecol];
    }
}

如果我有足够的声誉,我会发布图片。谢谢

最佳答案

最好使用 cellForItemAtIndexPath 来获取单元格。然后你可以使用标签来访问标签。 我认为您会在层次结构中得到另一个 View ,而不是您期望的标签。 (标签 0 对于所有 View 都是 true,调用返回第一个找到的)

最佳于尔根

关于iphone - UICollectionView 0th 索引改变完整 View 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18547884/

相关文章:

iphone - 标记国家边界 - iPad(带或不带 MapKit)

iOS:如何在没有 someView.addConstraints 的情况下使用 constraintsWithVisualFormat?

ios - 在数组中查找 float

objective-c - 谓词中对象的字典属性的元素的键路径?

iphone - NSDateFormatter 在设备和模拟器上给出不同的值?解决办法是什么?

iphone - UILabel 无法更新标签文本

iphone - 奇怪的错误 : wait_fences: failed to receive reply: 10004003

ios - Swift 4 - iOS 11 搜索栏范围不会出现,因为它应该

ios - 同一文件中 xcode "go back to last place"的快捷方式?

创建 NSRange 时的 Objective-C 错误消息