0

我正在使用 UICollectionViewController 来显示橙色背景的日子,当用户点击那一天时,那一天的背景应该变为灰色。该程序每天都可以正常工作,除了索引 path.row 值为 0 的星期一(它是系列中的第一项)。它将包括自身在内的所有视图的背景更改为橙色

    @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];
    }
}

}

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

4

2 回答 2

1

最好使用 cellForItemAtIndexPath 来获取单元格。然后您可以使用标签来访问标签。我认为您会在层次结构中返回另一个视图,而不是您预期的标签。(标记 0 对所有视图都为真,调用返回首先找到)

最佳尤尔根

于 2013-08-31T12:03:37.857 回答
0

您可能已将集合视图的标签设置为 0,更改您的集合视图的标签值

于 2014-09-29T05:48:50.750 回答