【问题标题】:Can't get my custom UITableViewCell to show无法显示我的自定义 UITableViewCell
【发布时间】:2014-06-30 23:13:01
【问题描述】:

我正在使用 TableView xib,所有代表和数据源似乎都运行良好。 如果我设置self.textLabel.text,它会正确显示表格视图的通用数量,但我需要显示我的自定义 TableViewCell。

我创建了一个 HistoryCell.xib,其中只有一个 tableviewcell。 我创建了一个 UITableViewCell 类“HistoryCell.h/HistoryCell.m”,并将其设置为 HistoryCell.xib 的文件所有者。 我将 UILabels 连接到 HistoryCell.h UILabel statusLabel UILabel nameLabel UILabel timeLabel

在我的主 ViewController 类中,cellForRowAtIndexPath

我正在输入

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

static NSString *CellIdentifier = @"historyCellType";
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[HistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.nameLabel.text = @"Donald Duck";

return cell;

}

我做错了什么?

谢谢!

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:

你应该像这样注册笔尖(可能在 viewDidLoad 中):

[self.tableView registerNib:[UINib nibWithNibName:@"HistoryCell" bundle:nil ] forCellReuseIdentifier:@"historyCellType"];

然后在你的 cellForRowAtIndexPath 方法中,使用这种不需要 if cell == nil 子句的新方法:

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

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyCellType" forIndexPath:indexPath];

cell.nameLabel.text = @"Donald Duck";

return cell;

}

【讨论】:

  • 这很聪明!但是,它在cellForRowAtIndexPath 的第一行一直崩溃 *** 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ setValue:forUndefinedKey:]:此类不符合键值编码键名标签。'
  • @Alan,这可能意味着您的 xib 文件有问题。确保您的插座确实称为nameLabel,并且如果您右键单击单元格以显示包含所有连接的窗口,则您没有任何黄色警告三角形。另外,在定义单元格后在 cellForRowAtIndexPath 中输入一个日志,以记录单元格,并确保返回一个 HistoryCell。
  • 没有任何代码,所以我猜测存在某种配置问题... UITableViewCell 是否假设在视图内?我只有 xib 内的 TableViewCell 和 UITableViewCell 内的 3 个 UILabel。另外,文件所有者是主控制器类还是“HistoryCell.h/HistoryCell.m”我检查了命名并且它们被正确命名,当我将鼠标悬停在 HistoryCell.h 中的连接点上时,它会突出显示正确的标签在xib...
  • @Alan,是的,它应该是 xib 中的一个裸 UITableViewCell (或子类)。至于文件的所有者,我通常将其保留为 NSObject,但我认为这并不重要。您是否将xib中单元格的类更改为HistoryCell?如果没有,你应该这样做。
  • 啊 X_X 我不知道发生了什么。是的,我确实将 UITableViewCell 设置为 HistoryCell,并且使用 ctrl-drag 将 UILabel 连接到 HistoryCell.h。我仍然遇到同样的错误。
【解决方案2】:

你需要反序列化实际的XIB数据:

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *cellnib = [[NSBundle mainBundle] loadNibNamed:@"HistoryXIBName" owner:self options:nil];
    cell = (HistoryCell *)[cellnib objectAtIndex:0];
}

否则它只是使用“默认”单元格。

编辑:另外,如果您使用情节提要,动态单元原型无需从 NIB 文件创建单元(如果您可以选择的话)。

编辑第二次: 你也可以试试这个(假设你使用的是 ARC):

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    HistoryCell *aNewCell = [[HistoryCell alloc] initWithNibName:@"HistoryXIBName" bundle:nil];
    cell = (HistoryCell *)*aNewCell.view;
}

在包含视图的 XIB 文件中,还有另一种方法使用您的单元格的插座,这比我在 iOS4 最新版本时使用的涉及更多。如果编辑后的版本不适合你,我会挖掘一个旧项目并记住我是如何做到的。

【讨论】:

  • 您好,感谢您的及时帮助!我试过这段代码,但应用程序在到达那里时崩溃:2013-03-21 17:00:36.212 AppName[3133:c07] *** 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ setValue:forUndefinedKey:]: 这个类不符合键名标签的键值编码。'
  • Alan:我编辑了第二种方法来尝试。另外,你能告诉我你的目标是什么版本的 iOS 吗?
  • 我的目标是 6.1。好的,我试试看。
  • 谢谢。新代码无法在HistoryCell *aNewCell = [[HistoryCell alloc] initWithNibName:@"HistoryCellView" bundle:nil]; 上编译它给出“'HistoryCell' 没有可见的@interface 声明选择器'initWithNibName:bundle:' 另外,我的 HistoryCell xib 是否假设在 View 中有一个 TableViewCell?我的 HistoryCell xib 只有裸露的 TableView Cell 就可以了。
  • 如果您的目标是 6.1,您可以在 rdelmar 的回答中使用 registerNib 方法。我假设因为您使用的是 NIB,所以您又回到了 4.X。正如我在回答中所说,Storyboards 为您完成所有这些工作,所以如果您可以使用它们,您应该使用它们。
【解决方案3】:

当您将 tableViewCell 与 xib 一起使用时,同时从 xib 加载实例化。

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

    static NSString *CellIdentifier = @"historyCellType";
    HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"HistoryCell"
                                         owner:nil
                                       options:nil]lastObject];
    }

    cell.nameLabel.text = @"Donald Duck";

    return cell;

    }

【讨论】:

    【解决方案4】:
    - (UITableViewCell *)tableView:(UITableView *)tableView 
           cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
        static NSString *cellidentifier=@"cell1"; 
        customcell *cell =
          (customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
    
        if (cell==nil)  {
    
        [tableView registerNib:[UINib nibWithNibName:@"Customcell" bundle:nil] 
                   forCellReuseIdentifier:cellidentifier]; 
    
        cell =
         (customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier 
                     forIndexPath:[NSIndexPath indexPathForItem:indexPath.row 
                                                      inSection:indexPath.section]];   
        }   
    
        return cell; 
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多