我尝试搜索一些关于如何填充表格 View 的教程,但我发现的都是陈旧和过时的视频。我试着从一个稍微更新的版本开始做,但它不起作用。我有
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[chemarray addObject:@"2"];
[chemarray addObject:@"test"];
[chemarray addObject:@"3"];
[chemarray addObject:@"science"];
}
和
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [chemarray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [chemarray objectAtIndex:indexPath.row];
return cell;
}
根据本教程,运行时,它应该显示我的数组中的项目,但对我来说,它没有!有谁知道如何解决这个问题?
最佳答案
你忘了 register单元格标识符的 Nib /类。
形成苹果UITableView
文档:
Important: You must register a class or nib file using the
registerNib:forCellReuseIdentifier:
orregisterClass:forCellReuseIdentifier:
method before calling thedequeueReusableCellWithIdentifier:forIndexPath:
method.
这是一个普通 UITableViewCell
的例子:
- (void) viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}
如果您没有注册 nib/类,方法 dequeueReusableCellWithIdentifier:forIndexPath:
本质上与 dequeueReusableCellWithIdentifier:
相同。而且它不会自动为您创建一个实例。
关于objective-c - 在 iOS 6 中填充 Tableview 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13063243/