我有一个非常烦人的问题,我不确定为什么会发生这种情况。
我有Table视图集,我在Cell中有正确的标识符,但它仍然让我“无法使用标识符Cell对单元格出列 - 必须为标识符注册一个nib或类或连接一个原型单元格一个故事板“当我运行它。
代码看起来像这样
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:@"name"];
return cell;
}
任何帮助都会很棒,因为我想撕开我的头发。该表连接到该类,然后Cell具有@“Cell”标识符
答案 0 :(得分:2)
重新检查所有4个步骤
第1步:
你的cellForRowAtIndexPath应该是这样的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:@"name"];
return cell;
}
第2步:您的“ViewController.m”文件应如下所示
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
第3步:
请检查 - 单击视图控制器移动到连接检查器 - 查看此检查器中是否存在任何其他不需要的连接。
第4步: Tableview单元属性检查器应该这样]
答案 1 :(得分:1)
而不是:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
尝试:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果在
之后不起作用UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
添加:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
答案 2 :(得分:1)
是的,您需要注册一个单元格才能重复使用。这可以在您创建表格视图后以编程方式完成:
static NSString *cellIdentifier = @"Cell";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
或者也可以通过选择单元格并导航到属性检查器并更改单元格的ID,在Interface Builder中完成此操作。
答案 3 :(得分:0)
您没有检查是否分配了单元格..您可以尝试以下代码..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
// add a placeholder cell while waiting on table data
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
}
答案 4 :(得分:0)
在文档大纲中,验证您的原型UITableViewCell
是UITableView
的孩子。
在“属性”检查器中,验证是否在故事板中正确设置了“单元格标识符”和“单元格样式”:
在Identity Inspector中,验证您是否将类设置为UITableViewCell
:
答案 5 :(得分:0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:@"name"];
}
return cell;
}