ios 在选择时更改 UITableViewCell 的边框颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17205873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change border color of a UITableViewCell on selection
提问by Dhanesh KM
I am using a custom table view cell for my tableview. For setting the border, I have put a view on the custom cell and I am changing its border properties.
我正在为我的 tableview 使用自定义 table view 单元格。为了设置边框,我在自定义单元格上放置了一个视图,并且正在更改其边框属性。
self.borderView.layer.borderColor = VIEW_BORDER_COLOR;
I want to highlight the selected cell by changing it's border color. I tried to change it in didselectrowforindexpath,
我想通过更改边框颜色来突出显示选定的单元格。我试图在 didselectrowforindexpath 中改变它,
cell.borderView.layer.borderColor = [UIColor yellowColor].CGColor;
but as the cells are reused it changes on scrolling.
但随着单元格被重用,它会在滚动时发生变化。
回答by Victor Sigler
Using:
使用:
Swift 2:
斯威夫特2:
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.grayColor().CGColor
Swift 3
斯威夫特 3
cell.layer.borderWidth = 2.0
cell.layer.borderColor = UIColor.gray.cgColor
回答by user2398911
You can use Objective-C
你可以使用 Objective-C
[cell.contentView.layer setBorderColor:[UIColor blackColor].CGColor];
[cell.contentView.layer setBorderWidth:2.0f];
Swift 5
斯威夫特 5
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 2.0
Hope it helps you.
希望对你有帮助。
回答by Ishank
Will have to mark/un-mark(assuming u need only one selected at a time) the border color again and again like-
必须一次又一次地标记/取消标记(假设您一次只需要选择一个)边框颜色,例如-
- (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;
}
}
Just save/cache the selected index like-
只需保存/缓存所选索引,例如-
- (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;
}