2013-12-14 56 views
0

所以我下面的教程,我试图找出如何显示自定义单元格 -自定义的UITableViewCell不正确更新

我定义一个类,它是负责每个细胞

#import <UIKit/UIKit.h> 

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

@end 

.m文件 -

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

这是我的表格绘制方法 -

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

这就是我在初始化resultsTuples和东西的地方。

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

我看到的仅仅是两个静态标签(名称和颜色),而不是从数组中填充任何东西。我检查了一些事实上正在设置 - 这是,所以我不知道为什么这不起作用。

任何人都可以帮忙吗?

+0

您面临的问题是什么? –

回答

0

您尚未加入_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]; 
+0

ahhh-没有看到 - 谢谢! – praks5432

0

只是你可以删除以下方法

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

和更新的cellForRowAtIndexPath

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

我不知道,但你可以试试看。