【发布时间】:2018-05-22 15:18:53
【问题描述】:
我无法显示海关单元格。下面我发布了我的代码的简化版本:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
NSLog(@"Being Called?");
//reuseID = @"newtableid";
self.backgroundColor = [UIColor redColor];
self.name = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)];
self.name.backgroundColor = [UIColor redColor];
[self addSubview:self.name];
return self;
}
在我的viewController中,我在ViewDidLoad方法中设置了tableView:
self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 400) style:UITableViewStylePlain];
self.table.delegate = self;
self.table.dataSource = self;
self.table.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.table];
[self.table registerClass:NewTableViewCell.class forCellReuseIdentifier:@"Cell"];
然后,我像这样完成 cellForRowAt 方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"Cell";
NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[NewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.backgroundColor = [UIColor blackColor];
cell.name.text = [_familyDetails objectAtIndex:indexPath.row];
return cell;
}
问题是,我的单元格上没有显示任何内容。我的 NewTableVIewCell 构造函数被调用,但单元格显示为空白(没有颜色、标签等)我做错了什么?
【问题讨论】:
-
“以编程方式创建自定义单元格”在问题中,但我认为
(void)awakeFromNib来自NewTableViewCell类?我们在哪里可以看到它的initWithStyle:reuseIdentifier:代码? -
您是否将 tableview 委托方法中的 numberOfRows 和 heightForRows 实现到您的 viewController 中?
标签: ios objective-c uitableview cell-formatting