ios - UIViewController 中的 Xcode 5.1 UITableView - 自定义 TableViewCell socket 为零

标签 ios uitableview storyboard

我在 UIViewController 中有一个 UITableView 作为 subview 。我没有使用 UITableViewController,因为我有一些与 tableview 无关的其他内容占据了屏幕的一部分。我正在使用 Storyboard。我将 tableview 设置为导出,viewcontroller 是数据源和委托(delegate)。如果我在这种情况下使用标准的 UITableViewCell,一切正常,我可以加载内容。

我需要为此 tableview 使用自定义 UITableViewCell,因此我执行了以下步骤:

  1. 创建了一个名为 SWTableViewCell 的 UITableView 子类
  2. 在 viewDidLoad 中添加了一个调用以使用界面构建器注册单元格:[self.tableView registerClass:[SWTableViewCell class] forCellReuseIdentifier:@"Cell"];
  3. 在我的 tableview 的 Storyboard中,将原型(prototype)单元格的类设置为 SWTableViewCell
  4. 在 Storyboard 中,将单元格重用标识符设置为“单元格”
  5. 为 Storyboard上的原型(prototype)单元格添加了一些标签
  6. 仍然在 Storyboard中,按住 ctrl 从标签拖动到 SWTableViewCell.h 以创建导出
  7. 在我的 cellForRowAtIndexPath: 方法中,将单元格出列并转换为自定义子类:SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];

当我检查它时,单元格变量是正确的类型。它不是 UITableViewCell,而是 SWTableViewCell。但是没有一个网点有人居住。属性存在,但它们为零。我确定我肯定遗漏了一个联系,但我想不出我在这一点上跳过了什么。

这种情况是否可能,或者只是无法通过 IB 和 Storyboard实现?

最佳答案

删除调用

[self.tableView registerClass:[SWTableViewCell class] forCellReuseIdentifier:@"Cell"];

解决问题。

我不知道这个方法到底有什么用,因为我从未使用过它,所以我会研究一下。如果有人知道此电话是否需要某事,请告诉我们!


编辑:

因此,registerClass:forCellReuseIdentifier: 背后的想法是告诉 tableView 如何创建新单元格。这样,如果 dequeueReusableCellWithIdentifier: 没有返回单元格(如果队列为空),tableView 知道如何创建一个新单元格,而您不需要手动创建单元格)。


编辑 2:

我找到了 this线程,其中说:

If the cell is created using prototypes within a storyboard it is not necessary to register the class.

只需调用 dequeueReusableCellWithReuseIdentifier: 即可“激活”tableView 的单元格重用功能。

因此 registerClass:forCellReuseIdentifier: 仅在您手动构建您的单元格时被调用,而不使用 Storyboard。

关于ios - UIViewController 中的 Xcode 5.1 UITableView - 自定义 TableViewCell socket 为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22352587/

相关文章:

ios - 获取 UISearchController 的 scopeButtonTitles 字符串

ios - 是否可以从我的应用程序中删除 HealthKit 条目?

ios - Xcode Interface Builder 错位 View /修复错位问题

objective-c - 我如何调整这个库以与 Storyboard一起使用?

ios - UIScrollView 内部的 UICollectionView

ios编程: Keeping user configuration in memory vs storage

ios - Xcode 尝试定位或生成匹配的签名 Assets 但未能成功

swift - 删除时为表格 View 单元格设置动画

iphone - UITableViewController 和 subview

ios - 如何在添加到 UIViewController 的 tableView 上使用 UIScrollViewDelegate 方法?