2012-02-10 36 views
0

我在UITableView中使用多個單元格類型的代碼在uitableview中使用多個單元格類型的問題

問題是單元格文本是不可見的。

代碼:

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 
} 

=========

頭:

#import <UIKit/UIKit.h> 

@interface SubscribeCell : UITableViewCell{ 
    UILabel *subscribeLabel; 
} 

@property(nonatomic, retain) UILabel *subscribeLabel; 

@end 

爲的cellForRowAtIndexPath以及細胞類代碼的代碼在下面給出而實現類:

#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

檢查subscribeLabel是否爲零。你在initWithNibName:bundle:創造,但與initWithFrame:reuseIdentifier:被初始化,所以它沒有達到您的標籤創建代碼。

+2

和subscribeLabel不是IBOutlet中或不加入細胞作爲子視圖 – NeverBe 2012-02-10 20:52:28

+0

良好的漁獲物,錯過了。 – 2012-02-10 20:59:13

+0

嘿..固定它@NeverBe – Ahsan 2012-02-10 21:03:07

1

如果我嘗試編譯你的代碼,我得到一個錯誤消息,指出的UITableViewCell未聲明調用方法「initWithNibName:捆綁:」。你應該使用正確的初始化方法'initWithStyle:reuseIdentifier:'。您也忘記將subscribeLabel添加到單元格的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; 
} 
相關問題