【问题标题】:Loading custom XIB for UITableViewCell为 UITableViewCell 加载自定义 XIB
【发布时间】:2014-07-29 08:13:25
【问题描述】:

我正在尝试为UITableViewCell 使用自定义 XIB 文件,但无论我做什么,表格单元格始终是空的 - 我的 XIB 根本没有加载。

没有任何错误。

这是我的工作:

  1. 创建一个空的应用程序,添加一个故事板,表格视图控制器,添加一个新的UITableViewController,将它与表格挂钩并设置一些示例项目。

  2. 创建一个控制器作为UITableViewCell的子类+检查选项以生成XIB。

  3. 创建三个示例标签并将它们添加为IBOutlets

  4. 加载 XIB 文件并尝试将其设置为单元格。

代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.delegate = self;

    items = [[NSMutableArray alloc] init];
    for (int i = 0; i < 20; i++) {
        ItemObject *item = [ItemObject new];
        item.topLabel = [NSString stringWithFormat:@"Top %d", i];
        item.leftLabel = [NSString stringWithFormat:@"Left %d", i];
        item.rightLabel = [NSString stringWithFormat:@"Right %d", i];
        [items addObject:item];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"ItemCell";

    // Set up the cell.
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    }
    ItemObject *currentObject = [items objectAtIndex:indexPath.row];
    cell.topLabel.text   = currentObject.topLabel;
    cell.leftLabel.text  = currentObject.leftLabel;
    cell.rightLabel.text = currentObject.rightLabel;

    // General cell and table settings.
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [tableView setSeparatorColor:[UIColor clearColor]];

    return cell;
}

错误可能在哪里?有人能指出我正确的方向吗?

谢谢!

【问题讨论】:

  • 阅读有关registerNib: 方法的文档。你会明白你做错了什么。
  • @duci9y 刚读了几遍,但我不太明白;你能澄清一下吗?这将有助于我在未来了解这些问题。
  • 来自文档:在将任何单元格出列之前 调用此方法或 registerClass:forCellReuseIdentifier: 方法来告诉表视图如何创建新单元格。
  • 感谢您的澄清 - 这是我期望的部分,但不确定。将记住这一点以供将来参考:D

标签: ios objective-c uitableview


【解决方案1】:

确保您已在您的 XIB 中设置了 ItemCell 标识符,并且您的表数据源也已设置。然后将您的registerNib 移动到viewDidLoad

-(void)viewDidLoad {
        //...
       self.tableview.dataSource = self;
       [self.tableView registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:@"ItemCell"];

        }

还有cellForRowAtIndexPath:

CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell" forIndexPath:indexPath];

并删除if (!cell) 块,无论如何它都不会被使用。

【讨论】:

  • 就是这样!设置了数据源以及单元格标识符 - 问题在于注册笔尖。一旦我将它移到viewDidLoad,它就完美地工作了。非常感谢,@spassas!
  • 这很有帮助。谢谢。 :)
猜你喜欢
  • 2015-07-19
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
  • 2017-10-01
  • 1970-01-01
相关资源
最近更新 更多