【问题标题】:Change border color of a UITableViewCell on selection在选择时更改 UITableViewCell 的边框颜色
【发布时间】:2023-03-06 14:51:01
【问题描述】:

我正在为我的表格视图使用自定义表格视图单元格。为了设置边框,我在自定义单元格上放置了一个视图,并且正在更改它的边框属性。

self.borderView.layer.borderColor = VIEW_BORDER_COLOR;

我想通过更改其边框颜色来突出显示所选单元格。我试图在 didselectrowforindexpath 中更改它,

cell.borderView.layer.borderColor = [UIColor yellowColor].CGColor;

但随着单元格被重复使用,它会在滚动时发生变化。

【问题讨论】:

  • 你在做多选还是单选...
  • [单元格设置ShowsTouchWhenHighlighted:YES];试试这个...

标签: ios user-interface uitableview


【解决方案1】:

使用:

斯威夫特 2:

cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.grayColor().CGColor

斯威夫特 3

cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.gray.cgColor

【讨论】:

    【解决方案2】:

    你可以使用 Objective-C

    [cell.contentView.layer setBorderColor:[UIColor blackColor].CGColor]; 
    [cell.contentView.layer setBorderWidth:2.0f]; 
    

    斯威夫特 5

    cell.layer.borderColor = UIColor.black.cgColor
    cell.layer.borderWidth = 2.0
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      将不得不一次又一次地标记/取消标记(假设您一次只需要选择一个)边框颜色 -

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
          if(indexPath.row==self.selectedRow){
      cell.borderView.layer.borderColor = [UIColor yellowColor].CGColor;
      }else {
      cell.borderView.layer.borderColor = [UIColor clearColor].CGColor;
      }
      }
      

      只需保存/缓存选定的索引,例如 -

          - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
      
      //Unselected the prevoius selected Cell
                  YourCellClass *aPreviousSelectedCell=  (YourCellClass*)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedRow inSection:0]];
                  aPreviousSelectedCell.borderView.layer.borderColor = [UIColor clearColor].CGColor;
      
      //Selected the new one-    
                 YourCellClass *aSelectedCell = (YourCellClass*)[tableView cellForRowAtIndexPath:indexPath];
      
          aSelectedCell.borderView.layer.borderColor = [UIColor yellowColor].CGColor; 
      
                  self.selectedRow = indexPath.row;
              }
      

      【讨论】:

      • 很高兴如果它有效..可以考虑接受正确的答案:)
      • borderView 不是 UITableViewCell 的有效属性
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多