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 に答える