无法更改自定义 UITableViewCell 中的透明 BG(分组表视图)
有许多关于 UITableViews(分组和普通)中单元格背景(自定义或标准)的各种问题的帖子,但我找不到任何提及此特定问题的内容。
基本上我有一个具有分组样式的 UITableView。
问题:我无法让自定义单元格具有透明背景以外的任何内容。
这是在实现自定义单元格之前我的表格视图的样子(即这只是一个普通的分组表格视图):
标准、正常分组的 Tableview。伟大的。但我想要定制单元格。
因此,我构建了一个如下所示的自定义单元格:
使用此层次结构:
单元格的背景元数据:
,无论我做什么,无论是将背景更改为白色、透明还是黑色,都没有什么区别,我的表格视图仍然看起来像这样:
(这是表格视图背景,fwiw。即这些单元格现在是透明的)。
当然,我无法通过更改添加到单元格的视图的背景来解决这个问题,因为这样所有的东西都会看起来像这样(注意没有圆角等) :
这显然是丑陋且不可接受的。
如何让单元格像标准默认单元格一样具有正常的白色背景?奇怪的是,对于上周的一个应用程序,我按照完全相同相同的过程制作了一个自定义单元格,并且这些背景显示为白色就好了。
我在这里错过了什么?我所需要的只是让我的自定义单元格具有符合分组表格视图的弯曲顶角和底角的白色背景。我在其他实现中看到白色背景,所以我知道使用自定义单元格是可能的。但这里有些东西不起作用!
我很高兴发布任何代码,只是不确定需要/想要什么。这是我的 cellForRowAtIndexPath 方法(这只是 tableView:cellForRowAtIndexPath 的核心数据版本:):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForManagedObject:(NSManagedObject *)managedObject
{
static NSString *ReuseIdentifier = @"customSummaryCell";
CustomSummaryTableViewCell *cell = (CustomSummaryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ReuseIdentifier];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomSummaryTableViewCell" owner:nil options:nil];
for (id currentObject in nibObjects) {
if([currentObject isKindOfClass:[CustomSummaryTableViewCell class]])
{
cell = (CustomSummaryTableViewCell *)currentObject;
}
}
}
cell.leftLabel.text = @"Some data";
cell.rightLabel.text = @"Some Info";
return cell;
}
我的类文件应该基本上没有变化,但它们就在这里。我的自定义单元格类的 .h 文件:
import
@interface CustomSummaryTableViewCell : UITableViewCell {
IBOutlet UIView *backgroundView;
IBOutlet UILabel *leftLabel;
IBOutlet UILabel *rightLabel;
}
@property (nonatomic, retain) IBOutlet UIView *backgroundView;
@property (nonatomic, retain) IBOutlet UILabel *leftLabel;
@property (nonatomic, retain) IBOutlet UILabel *rightLabel;
@end
和 .m 文件:
import "CustomSummaryTableViewCell.h"
@implementation CustomSummaryTableViewCell
@synthesize backgroundView;
@synthesize leftLabel;
@synthesize rightLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc
{
[leftLabel release];
[rightLabel release];
[backgroundView release];
[super dealloc];
}
@end
请注意,我已将 xib 中单元格的类更改为 CustomSummaryTableViewCell
任何帮助都会很大在这里表示赞赏,不知道问题是什么,这让我发疯!
提前致谢。
There are a number of posts regarding various issues with the backgrounds of cells (custom or standard) in UITableViews (grouped and plain), but I can find no mention of this specific issue.
Basically I have a UITableView with Grouped style.
The Problem: I can't get my custom cells to have anything other than a transparent background.
Here's what my tableview looks like before I implement my custom cells (i.e. this is just a normal grouped table view):
Standard, normal grouped Tableview. Great. But I want custom cells.
So I built a custom cell that looks like this:
With this hierarchy:
And this background metadata for the cell:
Unfortunately no matter what I do, whether changing background to white, clear, or even to black, nothing makes a difference, my table view still comes out looking like this:
(that's the table view background, fwiw. i.e. these cells are transparent now).
And, of course, I can't solve this by changing the background of the View I added to the cell, because then everything just comes out looking like this (notice the absence of rounded corners, etc):
Which is obviously ugly and unacceptable.
How do I get the cells to have a normal white background like the standard default cells?? The bizarre thing is that for an app last week I made a custom cell following exactly the same procedure and those backgrounds came out white just fine.
What have I missed here? All I need is for my custom cells to have a white background that conforms to the curved top and bottom corners of a grouped table view. I have seen the white background in other implementations, so I know it's possible with a custom cell. But something isn't working here!
I'm happy to post whatever code, just not sure what would be needed/wanted. Here's my cellForRowAtIndexPath method (this is just a core data version of tableView:cellForRowAtIndexPath:):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForManagedObject:(NSManagedObject *)managedObject
{
static NSString *ReuseIdentifier = @"customSummaryCell";
CustomSummaryTableViewCell *cell = (CustomSummaryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ReuseIdentifier];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomSummaryTableViewCell" owner:nil options:nil];
for (id currentObject in nibObjects) {
if([currentObject isKindOfClass:[CustomSummaryTableViewCell class]])
{
cell = (CustomSummaryTableViewCell *)currentObject;
}
}
}
cell.leftLabel.text = @"Some data";
cell.rightLabel.text = @"Some Info";
return cell;
}
My class files should be basically unchanged, but here they are. The .h file for my custom cell class:
import
@interface CustomSummaryTableViewCell : UITableViewCell {
IBOutlet UIView *backgroundView;
IBOutlet UILabel *leftLabel;
IBOutlet UILabel *rightLabel;
}
@property (nonatomic, retain) IBOutlet UIView *backgroundView;
@property (nonatomic, retain) IBOutlet UILabel *leftLabel;
@property (nonatomic, retain) IBOutlet UILabel *rightLabel;
@end
And the .m file:
import "CustomSummaryTableViewCell.h"
@implementation CustomSummaryTableViewCell
@synthesize backgroundView;
@synthesize leftLabel;
@synthesize rightLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc
{
[leftLabel release];
[rightLabel release];
[backgroundView release];
[super dealloc];
}
@end
Note that I have changed the class of the cell in the xib to CustomSummaryTableViewCell
Any help would be much appreciated here, not sure what the problem is and this is driving me nuts!!
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现向单元格添加子视图比实现自定义单元格更容易。就您而言,一个单元格中有两个标签,这看起来很容易做到。
编辑 - 简化示例
编辑 - 这是一个真实的完整示例:
I found that adding subviews to cell is easier than implementing custom cells. In your case, with two labels in one cell, that looks easy enough to do.
EDIT - Simplified example
EDIT - Here's a real, full example:
检查 nib 文件中 UITableViewCell 视图的 Alpha 和背景。
阿尔法应该是 1.0 。
Check the Alpha and Background of your UITableViewCell view in nib file.
The Alpha should be 1.0 .