无法在名称为'itemCell'的包中加载NIB

问题描述 投票:0回答:3

我无法加载我用tableviewcell类作为.xib的uitableviewcell。请提前帮助我。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellId = @"itemCell";

    NSDictionary *dictDetailList = [_arrItemList objectAtIndex:indexPath.row];

    HomeRestuTableViewCell *cell = (HomeRestuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellId];

    if(!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"itemCell" bundle:nil] forCellReuseIdentifier:cellId];
        cell = (HomeRestuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
    }
    [cell setupHomeRestuCell:dictDetailList];

    return cell;
}
ios objective-c uitableview
3个回答
1
投票

我认为你对Uitableviewcell的Cell Identifier和Register nib感到困惑

注册Nib用于将xib分配给您的单元格

[tableView registerNib:[UINib nibWithNibName:@"HomeRestuTableViewCell" bundle:nil] forCellReuseIdentifier:cellId];

因此,最好在viewController的ViewDidLoad中使用此行

和CellIdentifier用于识别单元格的类型,如表格视图中可以有多种类型的单元格,因此为了识别它们,TableView使用了CellReuseIdentifier

为表View注册单元格后,您可以使用以下方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellId = @"itemCell";

NSDictionary *dictDetailList = [_arrItemList objectAtIndex:indexPath.row];

HomeRestuTableViewCell *cell = (HomeRestuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellId];

if(!cell) {

    cell = [[HomeRestuTableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellId];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell setupHomeRestuCell:dictDetailList];

return cell;

希望能解决你的问题。


1
投票

在viewDidLoad中添加此行

确保重用标识符同时存在,并且您的bundle中应该有itemCell.xib

 [tableView registerNib:[UINib nibWithNibName:@"itemCell" bundle:nil] forCellReuseIdentifier:@"itemCell"];

0
投票

你已经使用了nib名称itemCell,这可能是错误的。通常你会将你的笔尖命名为与支持它的类相同,这意味着你需要加载那个命名的笔尖,即

[tableView registerNib:[UINib nibWithNibName:@"HomeRestuTableViewCell" bundle:nil] forCellReuseIdentifier:cellId];

没有要求您将nib命名为与类相同,因此请检查nib的文件名是什么,但如果它是HomeRestuTableViewCell.xib,并且它在您的主包中,则上述注册应该为您修复错误。

© www.soinside.com 2019 - 2024. All rights reserved.