更改所选UITableViewCell的边框颜色

我为我的tableview使用自定义表格视图单元格。 为了设置边框,我已经把自定义单元格的视图,我正在改变它的边界属性。

self.borderView.layer.borderColor = VIEW_BORDER_COLOR; 

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

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

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

使用:

Swift 2

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

Swift 3

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

您可以使用

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

希望它能帮助你。

将必须标记/取消标记(假设您只需要一次select一个)边框颜色一次又一次像 –

 - (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; } } 

只要保存/caching选定的索引像 –

  - (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; }