проблема с использованием нескольких типов ячеек в uitableview - PullRequest
0 голосов
/ 11 февраля 2012

Я использую этот код для нескольких типов ячеек в UITableView

Проблема в том, что текст ячейки невидим.Код для cellForRowAtIndexPath, а также код класса ячейки приведены ниже:

code:

static NSString *kCellIdentifier = @"NewsViewControllerTableCell";
static NSString *kCellIdentifier2 = @"SubscribeCell";


if ((indexPath.row==0) && ([[NSUserDefaults standardUserDefaults] boolForKey:@"subscribeButtonOption"]))
{
   SubscribeCell* cell = (SubscribeCell*)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier2];

    if (cell == nil) {
        cell = [[[SubscribeCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 35.0) reuseIdentifier:kCellIdentifier2] autorelease];
        cell.contentView.backgroundColor = kColorR53G53B53;
        cell.subscribeLabel.font = kLucidaSansStdFontBold_14;
        cell.subscribeLabel.textColor = [UIColor whiteColor];
    }

    cell.subscribeLabel.textColor=[UIColor redColor];
   cell.subscribeLabel.text = @"+ SUBSCRIBE TO NEWSLETTER";


   cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
   cell.selectedBackgroundView.backgroundColor =kColorR53G53B53;


   [cell setNeedsDisplay];
    return cell;           
}

else
{
   //another cell
 }

=========

header:

#import <UIKit/UIKit.h>

@interface SubscribeCell : UITableViewCell{
    UILabel *subscribeLabel;
}

@property(nonatomic, retain) UILabel *subscribeLabel;

 @end

и класс реализации:

#import "SubscribeCell.h"

@implementation SubscribeCell
@synthesize subscribeLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

        subscribeLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 323.0, 40.0)];
        subscribeLabel.textColor=[UIColor whiteColor];
        self.backgroundColor=kColorR53G53B53;

    }
    return self;
}

Ответы [ 2 ]

2 голосов
/ 11 февраля 2012

Проверьте, имеет ли значение subscribeLabel ноль. Вы создаете его в initWithNibName:bundle:, но инициализируете с помощью initWithFrame:reuseIdentifier:, поэтому он не достигает кода создания вашего ярлыка.

1 голос
/ 11 февраля 2012

Если я пытаюсь скомпилировать ваш код, я получаю сообщение об ошибке, в котором говорится, что UITableViewCell не объявляет метод, называемый initWithNibName: bundle:.Вы должны использовать правильный метод инициализации 'initWithStyle: reuseIdentifier:'.Вы также забыли добавить подписную метку в contentView ячейки.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        subscribeLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 323.0, 40.0)];
        subscribeLabel.textColor=[UIColor whiteColor];
        [self.contentView addSubview:subscribeLabel];
        self.backgroundColor=kColorR53G53B53;
    }
    return self;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...