具有自定义单元格的IOS静态表仅绘制随机单元格

use*_*608 1 static application-settings uitableview ios

我正在尝试为我的应用创建"设置"表格视图.我试图模仿它与Iphone上的gneral设置相同的风格.我通过继承UITableCell创建了自己的自定义单元类.我给了它适当的IBOulets,我把它们挂在故事板上.我也把开关连接到我的tableViewControler,但由于某种原因,我的代码只返回一个空单元格(只有一个单元格不是问题atm,因为我在我的设置中都有).我三重检查并确保我在我的代码和故事板中使用相同的单元格标识符.任何人都知道为什么我要回一个空白的单元格?

这是我的自定义单元格的.h文件.

@interface NHPSettingsCell : UITableViewCell
@property (nonatomic,weak) IBOutlet UILabel *settingLabel;
@property (nonatomic,strong) IBOutlet UISwitch *settingSwitch;
@end
Run Code Online (Sandbox Code Playgroud)

我的问题代码在这里,我的自定义单元格的.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
Run Code Online (Sandbox Code Playgroud)

我在自定义视图控制器中绘制单元格的方法:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

现在令我烦恼的是我已经成功地为使用自定义单元格的动态表实现了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;
}
Run Code Online (Sandbox Code Playgroud)

jrt*_*ton 8

两个问题:

  • 如果您使用静态单元格,请不要在视图控制器中实现任何数据源方法(numberOfRows,numberOfSections,cellForRow ...),因为这将覆盖您在故事板中构建的内容.该表包含您在故事板中提供的部分,行和内容.
  • 从故事板加载的单元格(动态原型或静态单元格)使用的初始化initWithCoder:,而不是initWithStyle:.awakeFromNib:是一个更好的放置代码的地方.