new*_*222 2 objective-c uitableview cell-formatting ios
我在让海关细胞出现时遇到问题。下面我发布了我的代码的简化版本:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
在我的 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"];
Run Code Online (Sandbox Code Playgroud)
然后,我像这样完成 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;
}
Run Code Online (Sandbox Code Playgroud)
问题是,我的手机上没有显示任何东西。我的 NewTableVIewCell 构造函数被调用,但单元格显示为空白(没有颜色、标签等)我做错了什么?
如果您以编程方式创建单元格,请确保将单元格注册到表格视图中。
[self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:"yourCellId"];
Run Code Online (Sandbox Code Playgroud)
UITableViewCell在这种情况下,我使用的是泛型。您可以尝试更改它的文本标签cellForRowAtIndexPath以查看是否有效。
如果需要,请务必将其更改为您的自定义单元格类。
| 归档时间: |
|
| 查看次数: |
2637 次 |
| 最近记录: |