在选择时更改UITableViewCell的边框颜色

Dha*_* KM 16 user-interface uitableview ios

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

self.borderView.layer.borderColor = VIEW_BORDER_COLOR;
Run Code Online (Sandbox Code Playgroud)

我想通过改变它的边框颜色来突出显示所选单元格.我试着在didselectrowforindexpath更改它,

cell.borderView.layer.borderColor = [UIColor yellowColor].CGColor;
Run Code Online (Sandbox Code Playgroud)

但随着细胞的重复使用,它会在滚动时发生变化.

Vic*_*ler 35

使用:

斯威夫特2:

cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.grayColor().CGColor
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.gray.cgColor
Run Code Online (Sandbox Code Playgroud)


小智 19

您可以使用

[cell.contentView.layer setBorderColor:[UIColor blackColor].CGColor]; 
[cell.contentView.layer setBorderWidth:2.0f]; 
Run Code Online (Sandbox Code Playgroud)

希望它对你有所帮助.


Ish*_*ank 5

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

- (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;
}
}
Run Code Online (Sandbox Code Playgroud)

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

    - (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;
        }
Run Code Online (Sandbox Code Playgroud)