所以我正在关注一个教程,我正在试图弄清楚如何显示自定义单元格 -
我已经定义了一个负责每个单元格的类 -
#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
上添加cell.contentView
UILabel。
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];
答案 1 :(得分: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"];
我不确定,但你可以尝试一下。