【问题标题】:Subclassed UItableViewCell selection子类化 UItableViewCell 选择
【发布时间】:2013-10-26 04:23:12
【问题描述】:

我对 UITableViewCell 进行了子类化,这样我就可以提高滚动性能,这对我来说效果很好。

在我的子类中,我有一个名为 seSelected 的方法,看起来像这样

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
    if (selected) {
        self.backgroundColor = [UIColor lightGrayColor];
    }else{
        self.backgroundColor = [UIColor whiteColor];
    }
}

我想知道如何制作它,以便如果我触摸同一个单元格,它会取消选择该单元格并将颜色改回白色?我在 setSelected 中尝试了一些不同的 if 语句,但没有任何效果。

【问题讨论】:

    标签: ios iphone objective-c uitableview


    【解决方案1】:

    使用这个委托方法

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    

    并在此处调用管理选定和未选定单元格的方法。

    【讨论】:

      【解决方案2】:

      一种方法是在单元格上设置标签,然后使用它来设置背景颜色。

      typedef enum {
          CellSelected = 0,
          CellDeselected
      } CellSelection;
      

      在创建单元格时,将标签设置为“CellDeselected”

      cell.tag  = CellDeselected;
      

      然后当点击单元格时,只需检查您要在背景中设置哪种颜色。

      switch (customCell.tag) {
          case CellSelected:
              self.backgroundColor = [UIColor lightGrayColor];
              break;
          case CellDeselected:
              self.backgroundColor = [UIColor whiteColor];
              break;
          default:
              break;
      }
      
      customCell.tag = !customCell.tag;
      

      【讨论】:

        【解决方案3】:

        这样做...

        -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
            str = [YourArray objectAtIndex:indexPath.row];//str is a string variable
        }
        
        -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
        
            NSString *cellIndentifier = @"cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier];
            }
            cell.textLabel.text = [reminder objectAtIndex:indexPath.row];
        
            cell.backgroundColor = [UIColor whiteColor];
        
            if ([str isEqualToString:[reminder objectAtIndex:indexPath.row]])
            {
                cell.backgroundColor = [UIColor grayColor];
            }
        
            return cell;
        }
        

        您可以将其用于您的自定义单元格.....

        【讨论】:

          【解决方案4】:

          使用 UITableViewCell 方法:[cell setSelectedBackgroundView:myBgColorView];。 Apple Documentation。 示例:

          UIView *myBgColorView = [[UIView alloc] init];
          myBgColorView.backgroundColor = [UIColor greenColor];
          [cell setSelectedBackgroundView:myBgColorView];
          

          【讨论】:

            【解决方案5】:

            设置单元格样式:

            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            

            您的代码将正常工作。但是,您只设置颜色选定的单元格。 如果您需要在按下单元格时设置颜色,请覆盖此方法:

            - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
            

            【讨论】:

              猜你喜欢
              • 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
              相关资源
              最近更新 更多