【问题标题】:UICollectionviewCell image change on selction选择时 UICollectionviewCell 图像更改
【发布时间】:2015-05-07 12:15:58
【问题描述】:

我在每个单元格中有UICollectionview 控制器和UIButton。在选择按钮时,我必须更改按钮 bg 图像,双击时我必须再次更改它。 (单击喜欢,双击喜欢用户兴趣页面上的概念)。 做这个的最好方式是什么?

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

    UICollectionViewCell *cell = [interestsCollection dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    UIButton* img=[[UIButton alloc]init];
    [img setImage:[UIImage imageNamed:[imgTitleArray objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
    [img addTarget: self action: @selector(interestClicked:) forControlEvents: UIControlEventTouchUpInside] ;
    img.frame=CGRectMake(10, 10, 60, 60);
    [cell.contentView addSubview:img];

    UILabel* lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 75, 80, 20)];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.text=[titleArray objectAtIndex:indexPath.row];
    lbl.font = [UIFont fontWithName:@"Arial" size:11];
    lbl.textColor = [UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1.0];
    [cell.contentView addSubview:lbl];

    return cell;
}

【问题讨论】:

  • 这个方法你实现了吗? -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {}
  • 是的,我有那个方法,但是如何在选择时更改选定单元格的子视图(按钮)背景?
  • -(void)interestClicked:(id)sender{ UIButton *button = (UIButton *)sender; //在此处更改按钮图像 [collectionView reloadData]; }

标签: ios objective-c uicollectionview uicollectionviewcell


【解决方案1】:

你可以用单TapGesture处理这种情况。在受人尊敬的选择器的帮助下,您可以更改 UIImageUIButton

这里是指导您的链接。如何在UICollectionView 上创建双击手势。借助此功能,您也可以创建单个 Tap 手势。

Collection View + Double Tap Gesture

【讨论】:

    【解决方案2】:

    UITapGestureRecognizer 添加到您的按钮并根据需要更改颜色。

      UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapOnce:)];
      UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapTwice:)];
    
      tapOnce.numberOfTapsRequired = 1;
      tapTwice.numberOfTapsRequired = 2;
    
      //stops tapOnce from overriding tapTwice
      [tapOnce requireGestureRecognizerToFail:tapTwice];
    
      [self.button addGestureRecognizer:tapOnce]; //remove the other button action which calls method `button`
      [self.button addGestureRecognizer:tapTwice];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多