在iOS 6中填充Tableview单元格

Ale*_*iop 16 objective-c uitableview

我已经尝试搜索一些关于如何填充表视图的教程,但我发现的只是旧的和过时的视频.我尝试从稍微更新的那个做,但它不起作用.我有

- (void)viewDidLoad
{
    [super viewDidLoad];
   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [chemarray addObject:@"2"];
    [chemarray addObject:@"test"];
    [chemarray addObject:@"3"];
    [chemarray addObject:@"science"];  
}
Run Code Online (Sandbox Code Playgroud)

- (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;
}
Run Code Online (Sandbox Code Playgroud)

根据本教程,在运行时,它应该显示我的数组中的项目,但对我来说,它不会!有谁知道如何解决这一问题?

rck*_*nes 24

您忘记为单元标识符注册 nib/class.

形成Apple UITableView文档:

要点:在调用方法之前,必须使用registerNib:forCellReuseIdentifier:or registerClass:forCellReuseIdentifier:方法注册类或nib文件 dequeueReusableCellWithIdentifier:forIndexPath: .

这是一个简单的例子UITableViewCell:

- (void) viewDidLoad {
   [super viewDidLoad];

   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}
Run Code Online (Sandbox Code Playgroud)

如果您没有注册nib/class,则该方法dequeueReusableCellWithIdentifier:forIndexPath:基本上与dequeueReusableCellWithIdentifier:.并且它不会自动为您创建实例.

  • 使用键盘,只需阅读文档即可. (3认同)
  • 等待这仍然不显示模拟器中的单元格 (2认同)