gpt4 book ai didi

ios - 在 uitableview 中使用多种单元格类型的问题

转载 作者:行者123 更新时间:2023-11-28 20:39:20 24 4
gpt4 key购买 nike

我将此代码用于 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;
}

最佳答案

检查 subscribeLabel 是否为 nil。您正在 initWithNibName:bundle: 中创建它,但正在使用 initWithFrame:reuseIdentifier: 进行初始化,因此它没有到达您的标签创建代码。

关于ios - 在 uitableview 中使用多种单元格类型的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9234081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com