简体   繁体   中英

Custom UITableViewCell not updating properly

So I'm following a tutorial and I'm trying to figure out how to show custom Cells -

I've defined a class that is in charge of each cell-

    #import <UIKit/UIKit.h>

@interface SearchResultCell : UITableViewCell
@property(copy,nonatomic)NSString *name;
@property(copy,nonatomic)NSString *colour;

@end

The .m file -

@implementation SearchResultCell{
    UILabel *_nameValue;
    UILabel *_colourValue;

}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CGRect nameLabelRect = CGRectMake(0,5,70,15);
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
        nameLabel.textAlignment = NSTextAlignmentLeft;
        nameLabel.text = @"Name:";
        nameLabel.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview: nameLabel];
        //so this is how I would add my rating view


        CGRect colourLabelRect = CGRectMake(0, 26,70, 15);
        UILabel *colourLabel = [[UILabel alloc] initWithFrame: colourLabelRect];
        colourLabel.textAlignment = NSTextAlignmentRight;
        colourLabel.text = @"Colour:";
        colourLabel.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview: colourLabel];

        CGRect nameValueRect = CGRectMake(80,5,200,15);
        _nameValue = [[UILabel alloc] initWithFrame:nameValueRect];
        CGRect colourValueRect = CGRectMake(80,25,200,15);
        _colourValue = [[UILabel alloc] initWithFrame:colourValueRect];
        [self.contentView addSubview: _colourValue];


    }
    return self;
}

- (void)setName:(NSString* )n {
    if(![n isEqualToString:_name]){
        _name = [n copy];
        _nameValue.text = _name;
    }
}

- (void)setColor:(NSString *)c
{
    if (![c isEqualToString:_colour]) {
        _colour = [c copy];
        _colourValue.text = _colour;
    } }

This is my table drawing method-

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchResultCell *cell = [self.resultTable dequeueReusableCellWithIdentifier:CellTableIdentifier];
    NSDictionary *rowData = self.resultsTuples[indexPath.row];
    cell.name = rowData[@"Name"];    
    cell.colour = rowData[@"Color"];
    return cell;
}

This is where I'm initializing the resultsTuples and stuff.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.resultsTuples = @[
                       @{@"Name" : @"MacBook", @"Color" : @"White"},
                       @{@"Name" : @"MacBook Pro", @"Color" : @"Silver"},
                       @{@"Name" : @"iMac", @"Color" : @"Silver"},
                       @{@"Name" : @"Mac Mini", @"Color" : @"Silver"},
                       @{@"Name" : @"Mac Pro", @"Color" : @"Silver"}];

        [self.resultTable registerClass:[SearchResultCell class] forCellReuseIdentifier: CellTableIdentifier];
    // Do any additional setup after loading the view.
}

What I see is just the two static labels (Name and Colour), rather than anything being populated from the array. I've checked that something is actually being set - and it is, so I'm not sure why this is not working.

Can anyone help?

You are not adding _nameValue UILabel on cell.contentView .

    CGRect nameValueRect = CGRectMake(80,5,200,15);
    _nameValue = [[UILabel alloc] initWithFrame:nameValueRect];

     //Add _nameValue label as well
     [self.contentView addSubview: _nameValue];

    CGRect colourValueRect = CGRectMake(80,25,200,15);
    _colourValue = [[UILabel alloc] initWithFrame:colourValueRect];
    [self.contentView addSubview: _colourValue];

just you can Remove Following methods

(void)setName:(NSString* )n { if(![n isEqualToString:_name]){ _name = [n copy]; _nameValue.text = _name; } }

(void)setColor:(NSString *)c { if (![c isEqualToString:_colour]) { _colour = [c copy]; _colourValue.text = _colour; } }

and update CellforRowatindexpath

cell.nameValue.text = rowData[@"Name"]; 
cell.colorValue.text = rowData[@"Color"];

I am not sure but you can give it a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM