单元格未出现在tableview中

Jon*_*man 2 iphone uitableview ios

我有一个历史页面,这是一个包含5行的UItableview.我已将原型单元格设置为我想要的规格,并将此文本添加到相应的historyviewcontroller.h文件中:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath           *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryItem"];
return cell;
} 
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,我没有看到任何单元格.我显然错过了一些东西,但我不太明白.

And*_*sek 5

您需要实际创建单元格.dequeueReusableCellWithIdentifier仅检索已创建的单元格,而不创建新单元格.

这是怎么做的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath           *)indexPath
    static NSString *CellIdentifier = @"HistoryItem"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //if cell is not nil, it means it was already created and correctly dequeued.
    if (cell == nil) {
        //create, via alloc init, your cell here
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    return cell;
}
Run Code Online (Sandbox Code Playgroud)