如何在使用UITableView registerNib时分配File的所有者:从nib加载自定义UITableViewCell?

Vil*_*nne 9 objective-c uitableview ios

所以我一直在考虑使用UITableView's registerNib:[dequeueReusableCellWithIdentifier: forIndexPath:]加载一个自定义UITableCellView的一个NIB.以下是我的控制器的重要部分:

- (void)viewDidLoad

[super viewDidLoad];
self.tableView.bounces = NO;
[self.tableView registerNib:[UINib nibWithNibName:@"ProgramListViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];



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

TVProgramListTableViewCell *cell = (TVProgramListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

cell.frame = CGRectMake(0, 0, CELLWIDTH, OPENCELLHEIGHT);
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.clipsToBounds = YES;

cell.titleLabel.text = [NSString stringWithFormat:@"herpa derp: %i", indexPath.row];

return cell;
Run Code Online (Sandbox Code Playgroud)

所以我正在注册NIB视图加载时,然后将其用于单元格出列.到目前为止,一切都像我期望的那样工作.我的自定义TVProgramListTableViewCell正在从中正确加载NIB并且IBOutlet正在连接.

NIB包含一个带有按钮的视图,我想向控制器发出激活事件.我可以将文件的所有者类型设置为我的表视图控制器类,但我不知道如何实际连接文件的所有者.

现在,如果我正在使用loadNibNamed:,并加载NIB自己,连接文件的所有者将很容易.有没有办法在利用时实现这一目标registerNib?除了无法连接文件所有者之外,这似乎是使用自定义单元格的完美方式UITableView.

rde*_*mar 2

据我所知,没有办法将文件的所有者设置为表视图控制器并将操作方法​​连接到 xib 文件中的按钮 - 我已经尝试过,但它使应用程序崩溃。通常完成此操作的方法是在 cellForRowAtIndexPath 方法中的按钮上调用 addTarget:action:forControlEvents: ,并将 self 作为目标传递。