无法在自定义UITableViewCell(分组表视图)中更改透明BG [英] Can't Change Transparent BG in Custom UITableViewCell (Grouped Table View)

查看:41
本文介绍了无法在自定义UITableViewCell(分组表视图)中更改透明BG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于UITableViews(分组和普通)中单元格背景(自定义或标准)的各种问题,有很多帖子,但是我找不到这个特定问题的消息.

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.

基本上,我有一个具有分组样式的UITableView.

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

标准,普通分组的Tableview.伟大的.但是我想要自定义单元格.

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

因此,我建立了一个自定义单元格,如下所示:

So I built a custom cell that looks like this:

具有此层次结构:

该单元格的背景元数据:

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).

当然,我不能通过更改添加到单元格中的 View 的背景来解决此问题,因为那样一来,一切都会以这种方式出现(请注意,没有圆角,等等):

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!

我很高兴发布任何代码,只是不确定是否需要/不需要什么.这是我的cellForRowAtIndexPath方法(这只是tableView:cellForRowAtIndexPath:的核心数据版本):

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

我的班级文件应该基本上没有变化,但是在这里.我的自定义单元格类的.h文件:

My class files should be basically unchanged, but here they are. The .h file for my custom cell class:

@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文件:

@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

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!!

谢谢.

推荐答案

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

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.

编辑-简化示例

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

这篇关于无法在自定义UITableViewCell(分组表视图)中更改透明BG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆