无法更改自定义 UITableViewCell 中的透明 BG(分组表视图)

发布于 2024-11-26 14:01:24 字数 3315 浏览 0 评论 0原文

有许多关于 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):

enter image description here

Standard, normal grouped Tableview. Great. But I want custom cells.

So I built a custom cell that looks like this:

enter image description here

With this hierarchy:

enter image description here

And this background metadata for the cell:

enter image description here

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:

enter image description here

(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):

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2024-12-03 14:01:24

我发现向单元格添加子视图比实现自定义单元格更容易。就您而言,一个单元格中有两个标签,这看起来很容易做到。

编辑 - 简化示例

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
    . . .
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, labelWidth, labelHeight)];
    [label2 setText:@"Something"];

    [[cell contentView] addSubview:label2];

    return cell;
}

编辑 - 这是一个真实的完整示例:

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
    static NSString *reusableCell = @"reusableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableCell];

    if(cell == nil)
    {        
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableCell] autorelease];

        thumbnail = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 106, 81)];

        feedTitle = [[UILabel alloc] initWithFrame:CGRectMake(116, 3, [IOSDevice screenWidth] - 140, 25)];
        postDate = [[UILabel alloc] initWithFrame:CGRectMake(116, 10, [IOSDevice screenWidth] - 140, 50)];
        description = [[UILabel alloc] initWithFrame:CGRectMake(116, 45, [IOSDevice screenWidth] - 140, 50)];

        // setting tag reminders so we can identify the element to replace
        [thumbnail setTag:1];
        [feedTitle setTag:2];
        [postDate setTag:3];
        [description setTag:4];

        [[cell contentView] addSubview:thumbnail];
        [[cell contentView] addSubview:feedTitle];
        [[cell contentView] addSubview:description];
        [[cell contentView] addSubview:postDate];

        [thumbnail release];
        [feedTitle release];
        [description release];
        [postDate release];
    }

    thumbnail = (UIImageView *)[[cell contentView] viewWithTag:1];
    feedTitle = (UILabel *)[[cell contentView] viewWithTag:2];
    postDate = (UILabel *)[[cell contentView] viewWithTag:3];
    description = (UILabel *)[[cell contentView] viewWithTag:4];

    [feedTitle setBackgroundColor:[UIColor clearColor]];
    [feedTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
    [feedTitle setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]];

    [description setBackgroundColor:[UIColor clearColor]];
    [description setFont:[UIFont fontWithName:@"Helvetica" size:12]];
    [description setTextColor:[UIColor colorWithRed:0.328 green:0.328 blue:0.328 alpha:1.0]];
    [description setNumberOfLines:2];
    [description setLineBreakMode:UILineBreakModeWordWrap];

    [postDate setBackgroundColor:[UIColor clearColor]];
    [postDate setFont:[UIFont fontWithName:@"Helvetica" size:12]];
    [postDate setTextColor:[UIColor colorWithRed:0.707 green:0.180 blue:0.141 alpha:1.0]];

    [thumbnail setImage:[[items objectAtIndex:[indexPath row]] objectForKey:@"thumb"]];

    [feedTitle setText:[[items objectAtIndex:[indexPath row]] objectForKey:@"title"]];
    [description setText:[[items objectAtIndex:[indexPath row]] objectForKey:@"summary"]];

    // Format date
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];

    [postDate setText:[dateFormatter stringFromDate:[[items objectAtIndex:[indexPath row]] objectForKey:@"date"]]];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    if([feedList contentOffset].y < -50)
    {
        shouldUpdate = TRUE;

        [activityIndicator stopAnimating];

        [feedList setContentOffset:CGPointMake(0, -30) animated:NO];
        [self loadData];

        loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -25, [IOSDevice screenWidth], 20)];
        [loadingLabel setText:@"Loading New Data"];
        [loadingLabel setTextAlignment:UITextAlignmentCenter];
        [loadingLabel setBackgroundColor:[UIColor clearColor]];
        [loadingLabel setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]];
        [loadingLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];

        reloadingSpinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(70, -25, 20, 20)];
        [reloadingSpinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [reloadingSpinner startAnimating];
        [reloadingSpinner setHidesWhenStopped:YES];

        [feedList addSubview:reloadingSpinner];
        [feedList addSubview:loadingLabel];
    }

    return cell;
}

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

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
    . . .
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, labelWidth, labelHeight)];
    [label2 setText:@"Something"];

    [[cell contentView] addSubview:label2];

    return cell;
}

EDIT - Here's a real, full example:

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
    static NSString *reusableCell = @"reusableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusableCell];

    if(cell == nil)
    {        
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableCell] autorelease];

        thumbnail = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 106, 81)];

        feedTitle = [[UILabel alloc] initWithFrame:CGRectMake(116, 3, [IOSDevice screenWidth] - 140, 25)];
        postDate = [[UILabel alloc] initWithFrame:CGRectMake(116, 10, [IOSDevice screenWidth] - 140, 50)];
        description = [[UILabel alloc] initWithFrame:CGRectMake(116, 45, [IOSDevice screenWidth] - 140, 50)];

        // setting tag reminders so we can identify the element to replace
        [thumbnail setTag:1];
        [feedTitle setTag:2];
        [postDate setTag:3];
        [description setTag:4];

        [[cell contentView] addSubview:thumbnail];
        [[cell contentView] addSubview:feedTitle];
        [[cell contentView] addSubview:description];
        [[cell contentView] addSubview:postDate];

        [thumbnail release];
        [feedTitle release];
        [description release];
        [postDate release];
    }

    thumbnail = (UIImageView *)[[cell contentView] viewWithTag:1];
    feedTitle = (UILabel *)[[cell contentView] viewWithTag:2];
    postDate = (UILabel *)[[cell contentView] viewWithTag:3];
    description = (UILabel *)[[cell contentView] viewWithTag:4];

    [feedTitle setBackgroundColor:[UIColor clearColor]];
    [feedTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
    [feedTitle setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]];

    [description setBackgroundColor:[UIColor clearColor]];
    [description setFont:[UIFont fontWithName:@"Helvetica" size:12]];
    [description setTextColor:[UIColor colorWithRed:0.328 green:0.328 blue:0.328 alpha:1.0]];
    [description setNumberOfLines:2];
    [description setLineBreakMode:UILineBreakModeWordWrap];

    [postDate setBackgroundColor:[UIColor clearColor]];
    [postDate setFont:[UIFont fontWithName:@"Helvetica" size:12]];
    [postDate setTextColor:[UIColor colorWithRed:0.707 green:0.180 blue:0.141 alpha:1.0]];

    [thumbnail setImage:[[items objectAtIndex:[indexPath row]] objectForKey:@"thumb"]];

    [feedTitle setText:[[items objectAtIndex:[indexPath row]] objectForKey:@"title"]];
    [description setText:[[items objectAtIndex:[indexPath row]] objectForKey:@"summary"]];

    // Format date
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];

    [postDate setText:[dateFormatter stringFromDate:[[items objectAtIndex:[indexPath row]] objectForKey:@"date"]]];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    if([feedList contentOffset].y < -50)
    {
        shouldUpdate = TRUE;

        [activityIndicator stopAnimating];

        [feedList setContentOffset:CGPointMake(0, -30) animated:NO];
        [self loadData];

        loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -25, [IOSDevice screenWidth], 20)];
        [loadingLabel setText:@"Loading New Data"];
        [loadingLabel setTextAlignment:UITextAlignmentCenter];
        [loadingLabel setBackgroundColor:[UIColor clearColor]];
        [loadingLabel setTextColor:[UIColor colorWithRed:0.215 green:0.215 blue:0.215 alpha:1.0]];
        [loadingLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];

        reloadingSpinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(70, -25, 20, 20)];
        [reloadingSpinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [reloadingSpinner startAnimating];
        [reloadingSpinner setHidesWhenStopped:YES];

        [feedList addSubview:reloadingSpinner];
        [feedList addSubview:loadingLabel];
    }

    return cell;
}
燃情 2024-12-03 14:01:24

检查 nib 文件中 UITableViewCell 视图的 Alpha 和背景。

阿尔法应该是 1.0 。

Check the Alpha and Background of your UITableViewCell view in nib file.

The Alpha should be 1.0 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文