objective-c - initWithNibName 到底做了什么?

标签 objective-c xcode uitableview

theTestController = [[[CustomCell alloc]initWithNibName:@"CustomCell" bundle:[NSBundle mainBundle]]autorelease];

我的猜测是,它将加载 CustomCell 并将 theTestController 设置为所有者。如果是这样:

  1. 为什么在 cellForRowAtIndexPath 的大多数示例代码中我看到 [[NSBundle mainBundle] loadNibNamed:@"CustomCell"owner: theTestController options:nil];而是?

  2. [[NSBundle mainBundle] loadNibNamed:@"CustomCell"owner: theTestController options:nil];theTestController = [[[CustomCell alloc]initWithNibName] 有什么区别:@"CustomCell"包:[NSBundle mainBundle]]autorelease];

  3. 我尝试用 theTestController = [[[CustomCell alloc]initWithNibName:@ “CustomCell”bundle:[NSBundle mainBundle]]autorelease]; 我收到错误。如果我使用后者,看起来 socket 仍然是nil

最佳答案

initWithNibName:bundle: 是在 UIViewController 中声明的便捷方法,可供其子类使用。这将通过加载 Nib 来初始化 View Controller ,可能是通过在内部使用 loadNibName:owner:options: 方法。

initWithNibName:bundle: 对于 UIView 及其子类不可用。因此我们必须使用 loadNibName:owner:options: 来加载 View 。

  1. 自定义单元格是 UIView 子类,因此使用 loadNibName:owner:options:
  2. 差别不大。 initWithNibName:bundle:UIViewController 初始化的便捷方法。
  3. 由于上述原因,您收到错误。

关于objective-c - initWithNibName 到底做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6401350/

相关文章:

python - 无法找出 cv.mat.step[0] 的 Numpy 等价物

ios - 如何使用 "sequential finger detection"的新 iOS 9.2 Touch ID 功能

iphone - 一个部分中的一个部分 - UITableView -

iphone - 以编程方式创建 UITextField 事件

ios - UITableViewCell 不水平裁剪内容

ios - UITableViewCell 中的 UICollectionView?

ios - 无法删除 uicollectionviewcell 底部的间距?

c++ - 如何验证 C++ 链表中的节点是否已解除分配?

objective-c - Cocoa 中的 Facebook 身份验证方法可以改进吗?

xcode - 使用swift在tableviewcell中创建tableview