我正在使用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];
}
}
}
如果我有足够的声誉,我会张贴图片。感谢
答案 0 :(得分:1)
最好使用cellForItemAtIndexPath来获取单元格。然后您可以使用标签访问标签。 我想你会在层次结构中找回另一个视图而不是你期望的标签。 (对于所有视图,标记0都为true,首先发现调用返回)
最佳Jürgen
答案 1 :(得分:0)
您可能已将集合视图的标记设置为0,更改了集合视图的标记值