问题描述
我已经阅读并尝试了我在 StackOverflow 上找到的一些答案.我还从博客中阅读并尝试了一些内容,但似乎没有任何内容可以实现我的目标.
I've read and tried a few answers I have found on StackOverflow. I've also read and tried a few things from blogs, but nothing seems to accomplish what I am looking for.
我创建了一个 UIView
并将它的背景颜色设置为我想要的 UITableViewCell
选择颜色 (而不是标准的蓝色或灰色选择颜色).我将此 UIView
添加到我的单元格的 selectedBackgroundView
并且这工作正常,我的单元格在用户选择时更改为所需的颜色.
I create a UIView
and set it's background color to my desired UITableViewCell
selection color (instead of the standard blue or gray selection colors). I add this UIView
to my cell's selectedBackgroundView
and this works fine, my cell changes to the desired color on user selection.
这个方法在普通 UITableViews
上效果很好;在 Grouped 上不太好.在分组的 UITableView
上,第一个和最后一个单元格不符合剪辑/蒙版边界,如下面的屏幕截图所示.
This method works great on Plain UITableViews
; not so well on Grouped. On a grouped UITableView
, the 1st and last cell do not conform to clip / mask bounds as demonstrated in the below screenshots.
我知道没有办法只圆左上角和右上角.
I know there is no way to round just the top-left and top-right corners only.
我想严格按照代码来做,没有图片.
I want to do this strictly by code, without images.
有谁知道只使用 UIView
而不是改变 UITableViewCell
的 selectedBackgroundView
颜色的好方法图像和使第一个和最后一个单元格符合圆角边界?
Does anyone know of a nice little work around to change the selectedBackgroundView
color of a UITableViewCell
using only the UIView
and not images AND to make the 1st and last cell conform to the rounded corner boundaries?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"Cell";
WCSBadgedCell * cell = [[WCSBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle andBadgeStyle:0 reuseIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[WCSBadgedCell alloc] initWithStyle:UITableViewCellStyleDefault andBadgeStyle:0 reuseIdentifier:CellIdentifier];
}
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:DARKBROWN];
[bgColorView setClipsToBounds: YES];
[cell.layer setMasksToBounds:YES];
[cell setSelectedBackgroundView:bgColorView];
[cell.textLabel setText: @"Testing a Cell"];
return cell;
}
截图
我接受了CodaFis 的回答,因为他添加了一条评论,指向一个非常好的(但很长) 解决方案.我不得不做很多改造,但最后,我现在有了我需要的 selectedBackgroundView,它位于第一个和最后一个单元格的拐角处,再次感谢!
I Accepted CodaFis answer because he added a comment which pointed to a pretty nice (yet lengthy) solution. I had to do quite a bit of revamping, but in the end, I now have the selectedBackgroundView's I needed which round the corners on the 1st and last cells, thanks again!
推荐答案
由于单元格的复杂性,我假设您正在使用 UITableViewCell 子类.这就是我一直在做的:
I assume that you are using a UITableViewCell subclass because of the complexity of your cell. This is how I've been doing it:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
{
self.clipsToBounds = YES;
UIView* bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.25f];
self.selectedBackgroundView = bgView;
//other code
}
return self;
}
这会在单元格上产生一种深灰色覆盖,不需要图像!
This produces a sort of dark grey overlay on the cell, not images required!
在您的情况下,您所选单元格的确切颜色(感谢方便的花花公子数字色度计)将是
In your case, the exact color of your selected cell (thanks to the handy dandy Digital Color Meter) would be
[UIColor colorWithRed:106.0f/255.0f green:51.0f/255.0f blue:6.0f/255.0f alpha:1.0f];
白色文本将是
- (void)setSelected:(BOOL)sel animated:(BOOL)animated
{
[super setSelected:sel animated:animated];
if (sel)
{
self.textLabel.textColor = [UIColor whiteColor];
}
else
{
self.textLabel.textColor = [UIColor colorWithRed:(105.f/255.f) green:(50.f/255.f) blue:(6.f/255.f) alpha:1.f];
}
}
这篇关于关于如何将 UIView 屏蔽为 UITableViewCell selectedBackgroundView 的明确答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!