ios 从 Nib 加载可重用的 UITableViewCell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/413993/
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
Loading a Reusable UITableViewCell from a Nib
提问by Greg Martin
I am able to design custom UITableViewCells and load them just fine using the technique described in the thread found at http://forums.macrumors.com/showthread.php?t=545061. However, using that method no longer allows you to init the cell with a reuseIdentifier which means you have to create whole new instances of each cell at every call. Has anyone figured out a good way to still cache particular cell types for reuses, but still be able to design them in Interface Builder?
我能够设计自定义 UITableViewCells 并使用http://forums.macrumors.com/showthread.php?t=545061 上的线程中描述的技术加载它们。但是,使用该方法不再允许您使用重用标识符初始化单元格,这意味着您必须在每次调用时为每个单元格创建全新的实例。有没有人想出一种仍然缓存特定单元格类型以供重用的好方法,但仍然能够在 Interface Builder 中设计它们?
采纳答案by Louis Gerbarg
Just implement a method with the appropriate method signature:
只需使用适当的方法签名实现一个方法:
- (NSString *) reuseIdentifier {
return @"myIdentifier";
}
回答by Tim Keating
Actually, since you are building the cell in Interface Builder, just set the reuse identifier there:
实际上,由于您是在 Interface Builder 中构建单元,只需在那里设置重用标识符:
Or if you are running Xcode 4, check the Attributes inspector tab:
或者,如果您正在运行 Xcode 4,请检查属性检查器选项卡:
(Edit: After your XIB is generated by XCode, it contains an empty UIView, but we need a UITableViewCell; so you have to manually remove the UIView and insert a Table View Cell. Of course, IB will not show any UITableViewCell parameters for a UIView.)
(编辑:XCode生成你的XIB后,它包含一个空的UIView,但我们需要一个UITableViewCell;所以你必须手动删除UIView并插入一个Table View Cell。当然,IB不会显示任何UITableViewCell参数界面视图。)
回答by wzs
Now, in iOS 5 there is an appropriate UITableView method for that:
现在,在 iOS 5 中有一个合适的 UITableView 方法:
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
回答by jrainbow
I can't remember where I found this code originally, but it's been working great for me so far.
我不记得我最初在哪里找到这段代码,但到目前为止它对我来说效果很好。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomTableCell";
static NSString *CellNib = @"CustomTableCellView";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
// perform additional custom work...
return cell;
}
Example Interface Builder setup...
示例界面生成器设置...
回答by Kendall Helmstetter Gelner
Look at the answer I gave to this question:
看我对这个问题的回答:
Is it possible to design NSCell subclasses in Interface Builder?
是否可以在 Interface Builder 中设计 NSCell 子类?
It's not only possible to design a UITableViewCell in IB, it's desirable because otherwise all of the manual wiring and placement of multiple elements is very tedious. Performaance is fine as long as you are careful to make all elements opaque when possible. The reuseID is set in IB for the properties of the UITableViewCell, then you use the matching reuse ID in code when attempting to dequeue.
在 IB 中设计 UITableViewCell 不仅是可能的,而且是可取的,因为否则所有手动布线和放置多个元素都非常繁琐。只要您尽可能小心地使所有元素不透明,性能就很好。在 IB 中为 UITableViewCell 的属性设置了重用 ID,然后在尝试出列时在代码中使用匹配的重用 ID。
I also heard from some of the presenters at WWDC last year that you shouldn't make table view cells in IB, but it's a load of bunk.
我还从去年 WWDC 的一些演讲者那里听说,你不应该在 IB 中制作表格视图单元格,但这是一堆废话。
回答by Ben Mosher
As of iOS circa 4.0, there are specific instructions in the iOS docs that make this work super-fast:
从 iOS 大约 4.0 开始,iOS 文档中有一些特定的说明可以使这项工作超快:
Scroll down to where it talks about subclassing UITableViewCell.
向下滚动到它谈论子类化 UITableViewCell 的地方。
回答by JDL
Here is another option:
这是另一种选择:
NSString * cellId = @"reuseCell";
//...
NSArray * nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:nil options:nil];
for (id obj in nibObjects)
{
if ([obj isKindOfClass:[CustomTableCell class]])
{
cell = obj;
[cell setValue:cellId forKey:@"reuseIdentifier"];
break;
}
}
回答by Mohit
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
return cell;
}
回答by Bill Garrison
This technique also works and doesn't require a funky ivar in your view controller for memory management. Here, the custom table view cell lives in a xib named "CustomCell.xib".
这种技术也有效,并且不需要在您的视图控制器中使用时髦的 ivar 来进行内存管理。在这里,自定义表格视图单元格位于名为“CustomCell.xib”的 xib 中。
static NSData *sLoadedCustomCell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
if (cell == nil)
{
if (sLoadedCustomCell == nil)
{
// Load the custom table cell xib
// and extract a reference to the cell object returned
// and cache it in a static to avoid reloading the nib again.
for (id loadedObject in [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil])
{
if ([loadedObject isKindOfClass:[UITableViewCell class]])
{
sLoadedCustomCell = [[NSKeyedArchiver archivedDataWithRootObject: loadedObject] retain];
break;
}
}
cell = (UITableViewCell *)[NSKeyedUnarchiver unarchiveObjectWithData: sLoadedCustomCell];
}
回答by ggarber
Louis method worked for me. This is the code I use to create the UITableViewCell from the nib:
路易斯方法对我有用。这是我用来从笔尖创建 UITableViewCell 的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellId"];
if (cell == nil)
{
UIViewController *c = [[UIViewController alloc] initWithNibName:@"CustomCell" bundle:nil];
cell = (PostCell *)c.view;
[c release];
}
return cell;
}