如何在点击UICollectionViewCell中更改图像?

时间:2014-09-18 13:30:59

标签: ios objective-c uicollectionview uicollectionviewcell

我正在尝试更改UICollectionViewCell内部显示的图像。这是我的代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:100];
    cellImageView.image = [[allActivities objectAtIndex:indexPath.row] obverseIcon];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@ was selected", [[allActivities objectAtIndex:indexPath.row] name]);

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:100];

    Activity *activity = [allActivities objectAtIndex:indexPath.row];

    if (activity.isMy)
    {
        activity.isMy = NO;
        cellImageView.image = [[allActivities objectAtIndex:indexPath.row] obverseIcon];
    }
    else
    {
        activity.isMy = YES;
        cellImageView.image = [[allActivities objectAtIndex:indexPath.row] reverseIcon];
    }

    [allActivities replaceObjectAtIndex:indexPath.row withObject:activity];
    [self.collectionView reloadData];
}

当我点按一个单元格时,图像不会改变。

3 个答案:

答案 0 :(得分:2)

试试这个:

- (UICollectionViewCellSubclass *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCellSubclass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    Activity *activity = allActivities[indexPath.row];
    cell.imageView.image = activity.isMy ? activity.obverseIcon : activity.reverseIcon;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    Activity *activity = allActivities[indexPath.row];
    activity.isMy = !activity.isMy;
    [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
}

其中UICollectionViewCellSubclass是您的自定义类,它继承自UICollectionViewCell并实现:

@property (nonatomic, strong) UIImageView *imageView;

答案 1 :(得分:0)

  1. 使用超类UICollectionViewCell创建自定义单元类。
  2. 从单元格的图像创建一个插座到该类.h文件。
  3. 在didSelectItemAtIndexPath方法中,替换UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@“Cell”forIndexPath:indexPath]; 用你的手机的班级名称。
  4. 更改图像。例如cell.imageView.hidden = YES;

答案 2 :(得分:-1)

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    questionImageCollectionViewCell * cell=[self.questionCollectionView dequeueReusableCellWithReuseIdentifier:@"coustomCell" forIndexPath:indexPath];
    cell.optionImageView.image=[UIImage imageNamed:[_optionImageArr objectAtIndex:indexPath.item]];
     [cell.rightTickImg setHidden:YES];
    if(self.array==nil)
        {
            [cell.rightTickImg setHidden:YES];
        }
    else
        {
            [cell.rightTickImg setHidden:YES];
            NSDictionary *dic = self.array[indexPath.row];
            NSLog(@"ouput DIC=%@",dic);
            NSLog(@"objeject for key=%@",[dic objectForKey:@"option"]);

   // if ([dic[@"option"] boolValue])
        if([[dic objectForKey:@"option"] isEqualToString:@"1"])

    {
        [cell.rightTickImg setHidden:NO];
    }
else
    {
  [cell.rightTickImg setHidden:YES];
    }

    }

return cell;

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return 

CGSizeMake(collectionView.frame.size.width/2.2,collectionView.frame.size.height/2.3);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{



     [self dicData];
    NSLog(@"First=%@",self.array);
     [self.array removeObjectAtIndex:indexPath.row];
    NSDictionary *dic = @{@"option":@"1"};
    [self.array insertObject:dic atIndex:indexPath.row];
    NSLog(@"Second=%@",self.array);

    NSMutableArray * indexArry=[[NSMutableArray alloc]init];
    for (NSInteger i=0; i< _optionImageArr.count; i++)
    {
        NSIndexPath * index =[NSIndexPath indexPathForRow:i inSection:0] ;
        [indexArry addObject:index];
        NSLog(@"%@",indexArry);


    }
     [collectionView reloadItemsAtIndexPaths:indexArry];


}
  1. 标题