0

UITableViews (グループ化およびプレーン) のセルの背景 (カスタムまたは標準) に関するさまざまな問題に関する投稿が多数ありますが、この特定の問題についての言及は見つかりません。

基本的に、グループ化されたスタイルの UITableView があります。

問題:カスタム セルの背景を透明にすることができません。

カスタム セルを実装する前のテーブルビューは次のようになります (つまり、これは通常のグループ化されたテーブル ビューです)。

ここに画像の説明を入力

標準、通常のグループ化されたテーブルビュー。偉大な。しかし、カスタムセルが必要です。

そこで、次のようなカスタム セルを作成しました。

ここに画像の説明を入力

この階層では:

ここに画像の説明を入力

そして、このセルのバックグラウンド メタデータ:

ここに画像の説明を入力

残念ながら、背景を白、透明、または黒に変更しても、何をしても違いはありません。テーブルビューは次のようになります。

ここに画像の説明を入力

(これはテーブル ビューの背景です。つまり、これらのセルは透明になりました)。

そしてもちろん、セルに追加したビューの背景を変更することでこれを解決することはできません。なぜなら、すべてがこのように見えるからです (丸みを帯びた角がないことに注意してください)。

ここに画像の説明を入力

これは明らかに醜く、受け入れられません。

標準のデフォルトセルのように、セルの背景を通常の白い背景にするにはどうすればよいですか?? 奇妙なことは、先週のアプリで、まったく同じ手順に従ってカスタムセルを作成したところ、それらの背景がうまく白くなったことです。

ここで何を見逃したのですか?必要なのは、グループ化されたテーブル ビューの湾曲した上隅と下隅に一致する白い背景を持つカスタム セルだけです。他の実装で白い背景を見たことがあるので、カスタム セルで可能であることはわかっています。しかし、ここで何かがうまくいきません!

どんなコードでも喜んで投稿しますが、何が必要で何が必要なのかわからないだけです。これが私の 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 ファイル:

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 に変更したことに注意してください。

ここで何か助けていただければ幸いです。問題が何であるかがわからず、これが私を夢中にさせています!!

前もって感謝します。

4

2 に答える 2

1

サブビューをセルに追加する方が、カスタム セルを実装するよりも簡単であることがわかりました。あなたの場合、1 つのセルに 2 つのラベルを使用すると、簡単に実行できます。

編集 - 簡単な例

-(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;
}
于 2011-07-26T05:17:46.030 に答える
0

nibファイルのUITableViewCellビューのアルファと背景を確認してください。

アルファは 1.0 である必要があります。

于 2011-07-26T05:13:28.347 に答える