ios 如何使用 UINib 实例化和使用自定义 UITableViewCells

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10527377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:19:00  来源:igfitidea点击:

How to use UINib to instantiate and use custom UITableViewCells

iosiphoneuitableviewcocoa-touchuikit

提问by inforeqd

how would i use UINibs to instantiate and use a UITableViewCell for a tableview in iOS5.0. I know there is a registerNib:forCellReuseIdentifier: in iOS5.0 that also needs to be used, but am not sure how to use it

我将如何使用 UINibs 实例化并使用 UITableViewCell 用于 iOS5.0 中的 tableview。我知道在 iOS5.0 中有一个 registerNib:forCellReuseIdentifier: 也需要使用,但不知道如何使用它

Thanks in advance for any help on this

提前感谢您对此的任何帮助

回答by jrturton

  1. Create your xib file with a UITableViewCell as the top-level object. This is called Cell.xib
  2. Create a UINib object based on this file
  3. Register the UINib with the table view (typically in viewDidLoad of your table view controller subclass).
  1. 使用 UITableViewCell 作为顶级对象创建您的 xib 文件。这称为 Cell.xib
  2. 基于这个文件创建一个 UINib 对象
  3. 使用表视图注册 UIInb(通常在表视图控制器子类的 viewDidLoad 中)。

Steps 2 and 3 can be combined, so you would use the following line in viewDidLoad:

步骤 2 和 3 可以合并,因此您将在 viewDidLoad 中使用以下行:

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

Then, in cellForRowAtIndexPath, if you want one of the cells from the nib, you dequeue it:

然后,在 cellForRowAtIndexPath 中,如果您想要来自笔尖的单元格之一,则将其出列:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

This either creates a new instance from the nib, or dequeues an existing cell.

这要么从笔尖创建一个新实例,要么使现有单元出列。

回答by Ortwin Gentz

@jrturtons answer is correct but unfortunately there's a bug in iOS 5 (fixed in iOS 6) in conjunction with VoiceOver: rdar://11549999. The following category on UITableViewfixes the issue. Just use -fixedDequeueReusableCellWithIdentifier:instead of the normal dequeueReusableCellWithIdentifier:. Of course, the NIB must be registered using

@jrturtons 的答案是正确的,但不幸的是,iOS 5(在 iOS 6 中已修复)与 VoiceOver 一起存在一个错误:rdar://11549999。以下类别UITableView解决了该问题。只需使用-fixedDequeueReusableCellWithIdentifier:而不是正常的dequeueReusableCellWithIdentifier:. 当然,必须使用 NIB 注册

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

before (in -viewDidLoad).

之前(中-viewDidLoad)。

UITableView+Workaround.m:

UITableView+Workaround.m:

@implementation UITableView (Workaround)
- (id)fixedDequeueReusableCellWithIdentifier:(NSString *)identifier {
    id cell = [self dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        // fix for rdar://11549999 (registerNib… fails on iOS 5 if VoiceOver is enabled)
        cell = [[[NSBundle mainBundle] loadNibNamed:identifier owner:self options:nil] objectAtIndex:0];
    }
    return cell;
}
@end