【问题标题】:Custom UITableViewCell selection style?自定义 UITableViewCell 选择样式?
【发布时间】:2012-08-08 20:20:03
【问题描述】:

当我单击我的UITableViewCell 时,当我单击单元格时,背景部分(我的背景图像未覆盖的区域)变为蓝色。此外,单击单元格上的所有UILabels 都会变成白色,这正是我想要的。

但是当我点击它时我不想要的是蓝色背景,但如果我点击selectionstylenone,那么我会丢失单元格中UILabels 的突出显示颜色。

那么有什么方法可以在单击单元格时摆脱蓝色背景,但保留UILabels 的突出显示颜色?

【问题讨论】:

标签: ios objective-c uitableview click selection


【解决方案1】:

您可以按如下方式执行此操作。将表格单元格的选择样式设置为UITableViewCellSelectionStyleNone。这将删除蓝色背景突出显示。然后,为了使文本标签突出显示以您想要的方式工作,而不是使用默认的 UITableViewCell 类,创建UITableViewCell 的子类并使用您自己的实现覆盖setHighlighted:animated 的默认实现,将标签颜色设置为您自己的想要取决于突出显示的状态。

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (highlighted) {
        self.textLabel.textColor = [UIColor whiteColor];
    } else {
        self.textLabel.textColor = [UIColor blackColor];
    }
}

【讨论】:

  • 附带说明,请确保覆盖setHighlighted:animated:,它采用animated 参数。覆盖一个参数setHighlighted: 方法将不起作用。
  • 我们应该打电话给[super setHighlighted:highlighted animated:animated]; 吗?
  • 非常感谢!奇迹般有效!如果我愿意,这让我可以控制在单元格中突出显示另一个自定义视图,因为标准突出显示这样做很奇怪。
  • 但是当你覆盖setHighlightedsetSelected时,这个方法有一个问题。首先选择一个单元格,确定 textColor 变为白色,然后将该单元格滚动到屏幕可视部分之外,然后向后滚动,看,textColor 变为黑色
  • @jonkroll 我理解这个概念,但我的要求是,当一个单元格被选中时,该单元格的 textLabel 的颜色应该保持为 selectedColor,现在它只是改变颜色选择时的textLabel,之后选择的textLabel保持原来的颜色
【解决方案2】:

如果在 iOS7 之前工作,请将您的单元格选择样式设为无

cell.selectionStyle = UITableViewCellSelectionStyleNone;

否则,请留下UITableViewCellSelectionStyleDefault

然后:

UIView *selectedView = [[UIView alloc]init];
selectedView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView =  selectedView;

此代码将正常工作

【讨论】:

  • @iBradApps...这里您没有创建任何自定义类... ui tableview 单元格
  • 对此表示赞成 - 这是唯一对我有用的解决方案,用于更改 iOS 7 中选定单元格的非文本颜色。
  • selectionstyle 应该是 selectionStyle
  • 此解决方案适用于 iOS7,但仅在我将 selectionStyle 设置为 UITableViewCellStyleDefault 之后。
  • 此方法是唯一与 Apple 的“开箱即用”单元格选择行为完全相同的方法。它应该被接受。
【解决方案3】:

将选择样式设置为无后,您可以使用以下委托方法:

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

在这里实现你的代码,像这样

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
    [cell.lbls setTextColor:[UIColor whiteColor]];
    return indexPath;
}

【讨论】:

  • 伟大而干净的解决方案!您只需要实现 willDeselectRowAtIndexPath 调用,它就像魔术一样工作!
  • 这里的一个可能的缺点是,直到您的手指离开所选单元格时才会调用此方法;其他一些方法会在您触摸单元格时立即生效。
【解决方案4】:

要完成这项工作,您必须将选择样式设置为UITableViewCellSelectionStyleNone,然后您应该重写方法setSelected:animated: 以获得您想要的结果。当您看到蓝色(或灰色)选择时,它的作用与 iOS 的自动选择机制相同。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (selected) {
        self.textLabel.textColor = [UIColor whiteColor];
    } else {
        self.textLabel.textColor = [UIColor blackColor];
    }
}

您还可以通过其他方式自定义它,例如通过改变 UITableViewCell 背景等。

【讨论】:

    【解决方案5】:

    在 cellForRowAtIndexPath 中使用以下代码:

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    [cell.myLabel setHighlightedTextColor: [UIColor whiteColor]]; // for all your labels
    

    希望这对你有用。

    享受编码:)

    【讨论】:

    • 这个答案需要放在首位。 :)
    • 这行不通。如果要制作 [cell.textLabel setHighlightedTextColor:[UIColor blueColor]];单元格 selectionStyle 不应为 NONE。
    【解决方案6】:

    UITableViewCell 的子类中覆盖以下函数。

    override func setHighlighted(highlighted: Bool, animated: Bool) { }
    override func setSelected(selected: Bool, animated: Bool) { }
    

    【讨论】:

    • 谢谢;这是唯一一个在更改背景颜色时不会选择从屏幕左边缘到右边缘的整行。
    • 我只是覆盖了 setSelected 这就是为什么由于某种原因单元格有时仍在闪烁的原因。
    【解决方案7】:

    为了匹配标准选择样式行为,您需要同时覆盖setHighlighted:animated:setSelected:animated:。您可能希望将该代码移动到共享方法中以避免重复代码。

    override func setHighlighted(highlighted: Bool, animated: Bool) {
    
        setAsSelectedOrHighlighted(highlighted, animated: animated)
        super.setHighlighted(highlighted, animated: animated)
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
    
        setAsSelectedOrHighlighted(selected, animated: animated)
        super.setSelected(selected, animated: animated)
    }
    
    func setAsSelectedOrHighlighted(selectedOrHighlighted: Bool, animated: Bool) {
    
        let action = {
            // Set animatable properties
        }
    
        if animated {
            UIView.animateWithDuration(1.0, delay: 0, options: .CurveEaseInOut, animations: action, completion: nil)
        }
        else {
            action()
        }
    }
    

    【讨论】:

      【解决方案8】:

      在您的自定义单元格中,覆盖 awakeFromNib 和 setSelected 的默认实现:

      - (void)awakeFromNib {
          // Initialization code
          UIImageView * imageView = [[UIImageView alloc] initWithFrame:self.bounds];
          imageView.image = [UIImage imageNamed:@"cell_selected_img"];
          self.selectedBackgroundView = imageView;
      }
      
      - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
          [super setSelected:selected animated:animated];
      
          // Configure the view for the selected state
          if (selected) {
              self.lblCustomText.textColor = [UIColor whiteColor];
          } else {
              self.lblCustomText.textColor = [UIColor blackColor];
          }
      }
      

      同时确保 选择样式 NOT 设置为 None

      【讨论】:

        【解决方案9】:

        我可以让它工作的唯一方法是:

        - (void)awakeFromNib {
            UIView *bgColorView = [[UIView alloc] init];
            bgColorView.backgroundColor = [UIColor colorWithRed:(55.0/255.0) green:(163.0/255.0) blue:(237.0/255.0) alpha:1.0];
            bgColorView.layer.masksToBounds = YES;
            self.selectedBackgroundView = bgColorView;
        }
        

        【讨论】:

          【解决方案10】:

          您也可以使用 contentView.alpha。这是一个例子。

          首先,为您的单元格设置选择样式:

          [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
          

          接下来,在自定义单元类中用动画示例覆盖此方法:

            - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
              [super setHighlighted:highlighted animated:animated];
          
              if (highlighted) {
                  [UIView animateWithDuration:0.15f animations:^{
                      self.contentView.alpha = 0.5f;
                  }];
              } else {
                  [UIView animateWithDuration:0.35f animations:^{
                      self.contentView.alpha = 1.f;
                  }];
              }
            }
          

          【讨论】:

            猜你喜欢
            • 2011-06-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-02-24
            • 2020-04-03
            相关资源
            最近更新 更多