【发布时间】:2012-09-07 07:51:21
【问题描述】:
每当我在表格视图中选择一个单元格时,单元格背景都会迅速变为蓝色。有没有办法将其更改为任何其他颜色并为每个单元格制作圆角?
【问题讨论】:
-
你不能创建这样的单元格
标签: iphone uitableview
每当我在表格视图中选择一个单元格时,单元格背景都会迅速变为蓝色。有没有办法将其更改为任何其他颜色并为每个单元格制作圆角?
【问题讨论】:
标签: iphone uitableview
你可以使用 UItableViewCell 属性
cell.selectionStyle=UITableViewCellSelectionStyleGray;
但你只有两个选择..蓝色或灰色
【讨论】:
使用cell.selectionStyle = UITableViewCellSelectionStyleGray; 或UITableViewCellSelectionStyleBlue。对于圆角情况,请使用
cell.layer.cornerRadius = 5;
【讨论】:
你好,试试这个可能对你有帮助...
cell.selectionStyle = UITableViewCellSelectionStyleGray; // this line for selection style
这里有3种选择样式
1.UITableViewCellSelectionStyleBlue
2.UITableViewCellSelectionStyleGray
3.UITableViewCellSelectionStyleNone
cell.layer.cornerRadius = 8; /// this line for round corner
希望对您有所帮助...
:)
【讨论】:
如果你使用cell.selectionStyle,那么你只能使用三种颜色。
如果您想使用更多颜色,请使用以下代码:
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
cell.selectedBackgroundView = myBackView;
[myBackView release];
【讨论】:
您可以在 selectRow 时将您的视图设置为您的单元格。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.selectedBackgroundView = YourView;
}
【讨论】:
创建一个新视图并将其分配给单元格的selectedBackgroundView 属性。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell
let backgroundView = UIView()
backgroundView.backgroundColor = .systemBlue
backgroundView.layer.cornerRadius = 10
cell!.selectedBackgroundView = backgroundView
return cell!
}
【讨论】: