我正在使用 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];
}
}
}
如果我有足够的声誉,我会发布图片。谢谢