UIStoryboard 中的自定义原型单元已创建但未显示在 UITableView 中
Custom prototype cell in UIStoryboard is created but doesn't show up in UITableView
我有一个项目需要使用自定义 UITableViewCell。我正在将单元格设计为情节提要中的原型,它在那里看起来不错。我将原型分配给我的自定义 UITableViewCell subclass,为其提供与我在 UITableView 中使用的相同的重用标识符,并将原型单元格上的 UILabel link 分配给我的 UITableViewCell sub[=19] 中的 IBOutlet =].
当我从 UITableView 调用它时,单元格被创建,如果我在该代码中添加标签和按钮 class(使用 CGRect 和所有创建它们)它们都可以工作,但标签我'已经添加到情节提要中永远不会出现。
我不明白如何成功调用和创建我的子class,但就我的应用而言,故事板中的布局和子视图似乎不存在。我做错了什么?
这是我的 cellForRowAtIndexPath 代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
// Configure the cell...
return cell;
}
您的 cellForIndexViewPath
应该如下所示,以创建带有标签的简单 custom cell
,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
确保你已经做好所有连接,将datasource
和delegate
设置为table
,然后将custom cell
的“标识符”设置为"MyTableViewCell" 在“属性检查器”中像这样,
故事板:
如上图所示添加 "MyTableViewCell" 而不是 "SimpleTableCell"。
acreichman,在 cellForRow 中添加转换并将 NSLog 放入单元格的 awakeFromNib 中以查看是否到达那里。让我知道...
我之前 运行 遇到过这个问题,在我的例子中,问题是视图控制器的自动生成代码包含对以下内容的调用:
[UITableView registerClass:forCellReuseIdentifier:]
我建议检查并删除对上述内容或
的任何调用
[UITableView registerNib:forCellReuseIdentifier:]
再次尝试您的原始代码。
我有一个项目需要使用自定义 UITableViewCell。我正在将单元格设计为情节提要中的原型,它在那里看起来不错。我将原型分配给我的自定义 UITableViewCell subclass,为其提供与我在 UITableView 中使用的相同的重用标识符,并将原型单元格上的 UILabel link 分配给我的 UITableViewCell sub[=19] 中的 IBOutlet =].
当我从 UITableView 调用它时,单元格被创建,如果我在该代码中添加标签和按钮 class(使用 CGRect 和所有创建它们)它们都可以工作,但标签我'已经添加到情节提要中永远不会出现。
我不明白如何成功调用和创建我的子class,但就我的应用而言,故事板中的布局和子视图似乎不存在。我做错了什么?
这是我的 cellForRowAtIndexPath 代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
// Configure the cell...
return cell;
}
您的 cellForIndexViewPath
应该如下所示,以创建带有标签的简单 custom cell
,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
确保你已经做好所有连接,将datasource
和delegate
设置为table
,然后将custom cell
的“标识符”设置为"MyTableViewCell" 在“属性检查器”中像这样,
故事板:
如上图所示添加 "MyTableViewCell" 而不是 "SimpleTableCell"。
acreichman,在 cellForRow 中添加转换并将 NSLog 放入单元格的 awakeFromNib 中以查看是否到达那里。让我知道...
我之前 运行 遇到过这个问题,在我的例子中,问题是视图控制器的自动生成代码包含对以下内容的调用:
[UITableView registerClass:forCellReuseIdentifier:]
我建议检查并删除对上述内容或
的任何调用[UITableView registerNib:forCellReuseIdentifier:]
再次尝试您的原始代码。