【问题标题】:All elements in custom UITableViewCell turn nil自定义 UITableViewCell 中的所有元素都变为 nil
【发布时间】:2015-07-10 04:39:57
【问题描述】:

我有一个自定义 UITableViewCell,名为 GenericRouteTableViewCell

@interface GenericRouteTableViewCell : UITableViewCell
@property (nonatomic, retain) IBOutlet UILabel *lblDirection;

我没有对实现文件做任何事情。

在我的故事板中,我有一个常规的 UITableViewController,带有 1 个原型单元。

  • 该原型单元的类:GenericRouteTableViewCell
  • 原型单元的重用标识符:GenericRouteTableViewCell
  • 我已经将标签连接到插座上,特别是没有将电池连接到任何地方
  • 我已将单元格高度设置为 100

在 UITableViewController 的代码中

  • viewDidLoad我打电话 [self.tableView registerClass:[GenericRouteTableViewCell class] forCellReuseIdentifier:@"GenericRouteTableViewCell"];
  • cellForRowAtIndexPath 我愿意:

    static NSString *cellIdentifier = @"GenericRouteTableViewCell";
    GenericRouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell.lblDirection setText:@"test"];
    

结果:

我只是得到空的默认单元格,当设置断点时,单元格被实例化,我看到我的标签被实例化,在它们被填充值之前它们被 nilled。

我做错了什么?

Xcode 6.3,最新 SDK

【问题讨论】:

  • 如果您在 Storyboard 中有单元格,则不应为单元格注册一个类。
  • 省略了行,但似乎没有任何效果

标签: ios uitableview


【解决方案1】:

您似乎没有为您的 tableView 添加委托。
UITableViewDelegateUITableViewDataSource

【讨论】:

  • UITableViewCell 我猜不需要这些代表?他们为什么会需要它们?
  • @Martijn 是的,当然。但它是 UITableView 所需要的。 UITableView 对象的委托必须采用 UITableViewDelegate 协议。该协议的可选方法允许委托管理选择、配置部分标题和页脚、帮助删除和重新排序单元格以及执行其他操作。 UITableViewDataSource 协议由一个对象采用,该对象为 UITableView 对象调解应用程序的数据模型。数据源为 table-view 对象提供构建和修改 table view 所需的信息。
  • 转念一想,这些代表似乎被排除在外了,奇怪。现在添加它们...
  • 所以请确保在 .h 文件中有 @interface ClassName : UIViewController 。在 viewDidLoad 中写入 _tableView.delegate = self; _tableView.dataSource = 自我
  • @Martijn 对你有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多