iphone - 无法更改自定义 UITableViewCell 中的透明 BG(分组 TableView )

标签 iphone ios uitableview custom-cell

有很多关于 UITableViews(分组和普通)中单元格背景(自定义或标准)的各种问题的帖子,但我找不到任何提及此特定问题的内容。

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

问题:我无法让自定义单元格拥有透明背景以外的任何内容。

这是在实现自定义单元格之前我的表格 View 的样子(即这只是一个普通的分组表格 View ):

enter image description here

标准、正常分组的表格 View 。伟大的。但我想要自定义单元格。

所以我构建了一个如下所示的自定义单元:

enter image description here

有了这个层次结构:

enter image description here

以及单元格的背景元数据:

enter image description here

不幸的是,无论我做什么,无论将背景更改为白色,透明,甚至黑色,都没有什么区别,我的表格 View 仍然看起来像这样:

enter image description here

(这是表格 View 背景,fwiw。即这些单元格现在是透明的)。

当然,我无法通过更改添加到单元格的 View 的背景来解决这个问题,因为这样所有的东西都会看起来像这样(注意没有圆角)等):

enter image description here

这显然是丑陋且 Not Acceptable 。

如何让单元格像标准默认单元格一样具有正常的白色背景?奇怪的是,对于上周的一个应用程序,我按照完全相同相同的过程制作了一个自定义单元,并且这些背景显示为白色,效果很好。

我在这里错过了什么?我所需要的只是让我的自定义单元格具有符合分组表格 View 的弯曲顶角和底角的白色背景。我在其他实现中看到白色背景,所以我知道使用自定义单元格是可能的。但这里有些东西不起作用!

我很高兴发布任何代码,只是不确定需要/想要什么。这是我的 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 文件:

导入

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

导入“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

这里非常感谢任何帮助,不知道问题是什么,这让我发疯!!

提前致谢。

最佳答案

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

编辑 - 简化示例

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

关于iphone - 无法更改自定义 UITableViewCell 中的透明 BG(分组 TableView ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6824770/

相关文章:

iphone - 如何使用 NSDate 构建一个 NSPredicate 过滤一个月中的所有天数?

iOS:Facebook SDK AppInvite 错误 9

ios - 长按 UIButton 上的手势识别器?

ios - Swift:Tableview 在导航栏下方滚动但在状态栏上方滚动?

ios - 我该如何修复或解决此 tableView textField 自动布局故障?

ios - 如何提取 JSON 数据以在 swift 2.0 的 UITableView 中动态应用?

iphone - 如何在 iPhone 中创建带有 2 个卷轴的 UI View

ios - App Store 是否接受自定义浏览器?

iphone - iTunes 连接版本摘要页面上的 "Territory"是什么

iOS 6 : Parent modal's modalPresentationStyle ignored after rotation