sg.*_*sg. 49 iphone background colors uitableview tableview
有没有人知道如何使用UITableViewCell为每个选定的单元格更改单元格的背景颜色?我在TableView的代码中创建了这个UITableViewCell.
Chi*_*ong 92
更改属性selectedBackgroundView是正确的,也是最简单的方法.我使用以下代码更改选择颜色:
// set selection color
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
cell.selectedBackgroundView = myBackView;
[myBackView release];
Run Code Online (Sandbox Code Playgroud)
loo*_*mer 38
我终于设法让这个工作在一个表格视图中,其样式设置为Grouped.
首先将selectionStyle所有单元格的属性设置为UITableViewCellSelectionStyleNone.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Run Code Online (Sandbox Code Playgroud)
然后在表视图委托中实现以下内容:
static NSColor *SelectedCellBGColor = ...;
static NSColor *NotSelectedCellBGColor = ...;
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];
if (currentSelectedIndexPath != nil)
{
[[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor:NotSelectedCellBGColor];
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:SelectedCellBGColor];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell.isSelected == YES)
{
[cell setBackgroundColor:SelectedCellBGColor];
}
else
{
[cell setBackgroundColor:NotSelectedCellBGColor];
}
}
Run Code Online (Sandbox Code Playgroud)
PK8*_*K86 19
// animate between regular and selected state
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected) {
self.backgroundColor = [UIColor colorWithRed:234.0f/255 green:202.0f/255 blue:255.0f/255 alpha:1.0f];
}
else {
self.backgroundColor = [UIColor clearColor];
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ens 13
经过一些测试此WILL除去取消选择或小区被轻触时表视图选择设定为"多重选择"第二次时的背景色.当表格视图样式设置为"Grouped"时也可以使用.
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.contentView.backgroundColor = UIColor.darkGray
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:为了使其能够如下所示工作,您的单元格的Selection属性可以设置为任何但是无.
风格:平原,选择:单一选择
风格:平原,选择:多重选择
风格:分组,选择:多重选择
要获得更平滑的颜色过渡,请尝试一些动画:
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
UIView.animate(withDuration: 0.3, animations: {
cell.contentView.backgroundColor = UIColor.darkGray
})
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可能会注意到选择单元格时图标和文本颜色也会发生变化.当您设置UIImage和UILabel突出显示属性时,会自动发生这种情况
的UIImage
的UILabel
只需为突出显示的属性提供颜色:
Sal*_*lim 11
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor yellowColor];
}
Run Code Online (Sandbox Code Playgroud)
Yer*_*han 10
我创建了UIView并设置了cell selectedBackgroundView的属性:
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = v;
Run Code Online (Sandbox Code Playgroud)
我对以下内容感到满意:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
bool isSelected = // enter your own code here
if (isSelected)
{
[cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
[cell setAccessibilityTraits:UIAccessibilityTraitSelected];
}
else
{
[cell setBackgroundColor:[UIColor clearColor]];
[cell setAccessibilityTraits:0];
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个高度定制的UITableViewCell.所以我实现了自己的细胞选择.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Run Code Online (Sandbox Code Playgroud)
我在我的单元格类中创建了一个方法:
- (void)highlightCell:(BOOL)highlight
{
if (highlight) {
self.contentView.backgroundColor = RGB(0x355881);
_bodyLabel.textColor = RGB(0xffffff);
_fromLabel.textColor = RGB(0xffffff);
_subjectLabel.textColor = RGB(0xffffff);
_dateLabel.textColor = RGB(0xffffff);
}
else {
self.contentView.backgroundColor = RGB(0xf7f7f7);;
_bodyLabel.textColor = RGB(0xaaaaaa);
_fromLabel.textColor = [UIColor blackColor];
_subjectLabel.textColor = [UIColor blackColor];
_dateLabel.textColor = RGB(0x496487);
}
}
Run Code Online (Sandbox Code Playgroud)
在ViewWillAppear的UITableViewController类中添加了以下内容:
NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];
SideSwipeTableViewCell *cell = (SideSwipeTableViewCell*)[self.tableView cellForRowAtIndexPath:tableSelection];
[cell highlightCell:NO];
Run Code Online (Sandbox Code Playgroud)
在didSelectRow中添加了这个:
SideSwipeTableViewCell *cell = (SideSwipeTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
[cell highlightCell:YES];
Run Code Online (Sandbox Code Playgroud)
对于iOS7 +,如果您使用的是Interface Builder,则将您的单元格子类化并实现:
Objective-C的
- (void)awakeFromNib {
[super awakeFromNib];
// Default Select background
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor redColor];
self.selectedBackgroundView = v;
}
Run Code Online (Sandbox Code Playgroud)
Swift 2.2
override func awakeFromNib() {
super.awakeFromNib()
// Default Select background
self.selectedBackgroundView = { view in
view.backgroundColor = .redColor()
return view
}(UIView())
}
Run Code Online (Sandbox Code Playgroud)
这与分组调用完美配合:实现自定义子类 UITableViewCell
这将尊重角落等......
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if(selected)
[self setBackgroundColor:[UIColor colorWithRed:(245/255.0) green:(255/255.0) blue:(255/255.0) alpha:1]];
else
[self setBackgroundColor:[UIColor whiteColor]];
}
Run Code Online (Sandbox Code Playgroud)
如果您只想删除灰色背景颜色,请执行以下操作:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setSelectionStyle:UITableViewCellSelectionStyleNone];
}
Run Code Online (Sandbox Code Playgroud)
我能够通过创建UITableViewCellsetSelected:animated:方法的子类并实现它来解决这个问题
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if(selected) {
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
[self setBackgroundColor:[UIColor greenColor]];
} else {
[self setBackgroundColor:[UIColor whiteColor]];
}
}
Run Code Online (Sandbox Code Playgroud)
诀窍是设置
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
Run Code Online (Sandbox Code Playgroud)
在实现视图控制器中然后在tableViewCell中将其设置为
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.:)