gpt4 book ai didi

ios - 关于如何将 UIView 屏蔽为 UITableViewCell selectedBackgroundView 的明确答案

转载 作者:可可西里 更新时间:2023-11-01 03:15:09 24 4
gpt4 key购买 nike

我已经阅读并尝试了一些在 StackOverflow 上找到的答案。我也阅读并尝试了博客中的一些东西,但似乎没有任何东西可以实现我正在寻找的东西。

我创建了一个 UIView 并将其背景颜色设置为我想要的 UITableViewCell 选择颜色 (而不是标准的蓝色或灰色选择颜色) .我将这个 UIView 添加到我的单元格的 selectedBackgroundView 并且这工作正常,我的单元格在用户选择时更改为所需的颜色。

此方法在 Plain UITableViews 上效果很好;在分组上不太好。在分组的 UITableView 上,第一个和最后一个单元格不符合剪辑/ mask 边界,如下面的屏幕截图所示。

我知道没有办法仅将左上角和右上角四舍五入。

我想严格地通过代码来做到这一点,没有图像。

问题

有没有人知道一个不错的小工作来改变 UITableViewCellselectedBackgroundView 颜色只使用 UIView 而不是图像并使第一个和最后一个单元格符合圆角边界?

例子

- (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;
}

截图

enter image description here

enter image description here

enter image description here

解决方案

我接受了 CodaFis answer因为他添加了一条评论,指出了一个非常好的(但冗长) 解决方案。我不得不做很多修改,但最后,我现在有了我需要的 selectedBackgroundView,它位于第一个和最后一个单元格的拐角处,再次感谢!

Here is a n example of how I achieved this.

最佳答案

由于单元格的复杂性,我假设您正在使用 UITableViewCell 子类。这就是我一直在做的:

- (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;
}

这会在单元格上产生一种深灰色覆盖层,而不是所需的图像!

在你的例子中,你选择的单元格的确切颜色(多亏了方便的花花公子数字颜色计)将是

[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];

}
}

关于ios - 关于如何将 UIView 屏蔽为 UITableViewCell selectedBackgroundView 的明确答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10219560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com