我在UITableView
中使用此代码用于多个单元格类型问题是单元格文本是不可见的。 cellForRowAtIndexPath的代码以及单元类代码如下:
代码:
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
和实现类:
#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;
}
答案 0 :(得分:2)
检查subscribeLabel是否为零。您是在initWithNibName:bundle:
中创建的,但正在使用initWithFrame:reuseIdentifier:
进行初始化,因此未达到您的标签创建代码。
答案 1 :(得分:1)
如果我尝试编译你的代码,我会收到一条错误消息,指出UITableViewCell没有声明一个名为'initWithNibName:bundle:'的方法。您应该使用正确的初始化方法'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;
}