我正在尝试为我的应用创建“设置”表格视图。我试图模仿它与Iphone上的gneral设置相同的风格。我通过继承UITableCell创建了自己的自定义单元类。我给了它适当的IBOulets,我把它们挂在故事板上。我也把开关连接到我的tableViewControler,但由于某种原因,我的代码只返回一个空单元格(只有一个单元格不是问题atm,因为我在我的设置中都有)。我三重检查并确保我在我的代码和故事板中使用相同的单元格标识符。任何人都知道为什么我要回一个空白的单元格?
这是我的自定义单元格的.h文件。
@interface NHPSettingsCell : UITableViewCell
@property (nonatomic,weak) IBOutlet UILabel *settingLabel;
@property (nonatomic,strong) IBOutlet UISwitch *settingSwitch;
@end
我的问题代码在这里,我的.h文件是自定义单元格:
#import "NHPSettingsCell.h"
@implementation NHPSettingsCell
@synthesize settingLabel, settingSwitch;
- (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
}
@end
我在自定义视图控制器中绘制单元格的方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SettingsCell";
NHPSettingsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[NHPSettingsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellStyleDefault;
}
cell.settingLabel.text = @"firstSetting";
//check the if user wants promt of disclaimer each time or not.
if([prefs boolForKey:@"firstSetting"] == YES){
cell.settingSwitch.on = YES;
}else{
cell.settingSwitch.on = NO;
}
return cell;
}
现在令我烦恼的是我已成功设法为使用自定义单元格的动态表实现cellForRowAtIndexPath方法。我还使用默认单元格实现静态表的代码,但对于具有自定义单元格的静态表,它似乎不起作用。这是关于如何在动态表上实现我的自定义单元格的代码(注意我不必初始化单元格但是它有效)。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"InteractionResultCell";
NHPResultCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure & Fill the cell
cell.leftLabel.text = [[resultsList objectAtIndex:indexPath.row] substanceName];
cell.rightLabel.text = [[resultsList objectAtIndex:indexPath.row] substanceName2];
NSString *color = [NSString stringWithFormat:@"%@", [[resultsList objectAtIndex:indexPath.row] color]];
//Change a hex value to a readable 0x number to pass ot hte macro so we can go from a hex color to a RGB system.
NSScanner *scanner;
unsigned int tempint=0;
scanner = [NSScanner scannerWithString:color];
[scanner scanHexInt:&tempint];
cell.severityButton.backgroundColor = UIColorFromRGB(tempint);
return cell;
}
答案 0 :(得分:8)
两个问题:
initWithCoder:
初始化从故事板(动态原型或静态单元格)加载的单元格,而不是initWithStyle:
。 awakeFromNib:
是放置设置代码的更好地方。 答案 1 :(得分:0)
dequeueReusableCellWithIdentifier:
才有效。如果不先创建单元格,则无法重复使用单元格。 xib
中创建的静态单元格是默认类型。这就是为什么它不适用于具有自定义单元格的静态表。在重用后添加单元格创建代码,就像在自定义视图控制器的cellForRowAtIndexPath:
方法中一样:
if (cell == nil) {
cell = [[NHPSettingsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellStyleDefault;
}
编辑要init
自定义单元格,您必须从xib
加载。将以下类方法添加到NHPSettingsCell.m
:
+(NHPSettingsCell*) createTextRowWithOwner:(NSObject*)owner{
NSArray* wired = [[NSBundle mainBundle] loadNibNamed:@"NHPSettingsCell" owner:owner options:nil];
NHPSettingsCell* cell = (NHPSettingsCell*)[wired firstObjectWithClass:[NHPSettingsCell class]];
return cell;
}
然后从您的自定义视图控制器中调用它:
cell = (NHPSettingsCell*)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (Nil == cell) {
cell = [NHPSettingsCell createTextRowWithOwner:self];
}