objective-c - 在 iOS 6 中填充 Tableview 单元格

标签 objective-c uitableview

我尝试搜索一些关于如何填充表格 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: or registerClass:forCellReuseIdentifier: method before calling the dequeueReusableCellWithIdentifier: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/

相关文章:

ios - 将事件添加到日历非常慢

ios - 如何在 Swift 中以编程方式 100% 创建自定义单元格?

ios - 在 Storyboard中将 UITableView 的 rowHeight 设置为 UITableViewAutomaticDimension?

ios - 从表 XIB 识别并导航到 Storyboard View Controller

ios - NSOperationQueue addOperationWithBlock 返回mainQueue的操作顺序

objective-c - 想在 iOS 应用中使用 sqlite3.h

objective-c - 使用 Parse.com 查询数组中的 2 个值

Objective-c函数指针

ios - 使用 Scrollview 的 contentOffset 计算 Imageview Frame

ios - UITableViewCell 内的 UIView 上的 UIGestureRecognizer