Search code examples
iosuitableviewstaticapplication-settings

IOS Static Table with Custom Cell only draws a random cell


I am trying to create a "settings" table view for my app. I am trying to mimic it to be the same style as the gneral setting on an Iphone. I have created my own custom cell class by inheriting from UITableCell. I gave it the appropriate IBOulets and i have hooked them up in the storyboard. I also hooked up the switch to my tableViewControler, but for some reason my code is only returning me one empty cell (it being only one cell is not an issue atm for that's all i have in my setting). I triple checked and made sure that I'm using the same cell identifier in my code and in storyboard. Anyone know why I'm getting a blank cell back?

Here is my .h file for my custom cell.

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

MY Problem code is here, my .h file for the custom cell:

#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

My method for drawing the cell in my custom view controller:

- (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;
}

Now the thing that annoys me is i have successfully managed to implement the cellForRowAtIndexPath method for a dynamic table that uses custom cells. I have also implements the code for a static table using the default cell, but for a static table with custom cells it just doesn't seem to work. Here is the code on how I implemented my custom cells on a dynamic table (note how i didn't have to init the cells but it works).

- (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;
}

Solution

  • Two problems:

    • If you are using static cells, do not implement any datasource methods in your view controller (numberOfRows, numberOfSections, cellForRow...) as this will override what you have built in the storyboard. The table has the sections, rows and content you give it in the storyboard.
    • Cells loaded from the storyboard (either dynamic prototypes, or static cells) are initialised using initWithCoder:, not initWithStyle:. awakeFromNib: is a better place to put your set up code.