如何从Xib文件加载自定义UITableViewCells? [英] How do you load custom UITableViewCells from Xib files?

查看:89
本文介绍了如何从Xib文件加载自定义UITableViewCells?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单:如何从Xib文件加载自定义UITableViewCells?这样做允许您使用Interface Builder来设计单元格。答案显然不是简单的,由于记忆管理问题。 此主题提及问题并建议一个解决方案,但是预先NDA释放和缺乏码。以下是一个长线程,无需提供确定的回答。

The question is simple: How do you load custom UITableViewCells from Xib files? Doing so allows you to use Interface Builder to design your cells. The answer apparently is not simple due to memory managment issues. This thread mentions the issue and suggests a solution, but is pre NDA-release and lacks code. Here's a long thread that discusses the issue without providing a definitive answer.

以下是我使用的一些代码:

Here's some code I've used:

static NSString *CellIdentifier = @"MyCellIdentifier";

MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = (MyCell *)[nib objectAtIndex:0];
}

要使用此代码,请创建MyCell.m / .h的UITableViewCell并为您想要的组件添加IBOutlet。然后创建一个新的Empty XIB文件。在IB中打开XIB文件,添加一个UITableViewCell对象,将其标识符设置为MyCellIdentifier,并将其类设置为MyCell并添加您的组件。最后,将IBOutlet连接到组件。注意,我们没有在IB中设置文件的所有者。

To use this code, create MyCell.m/.h, a new subclass of UITableViewCell and add IBOutlets for the components you want. Then create a new "Empty XIB" file. Open the Xib file in IB, add a UITableViewCell object, set its identifier to "MyCellIdentifier", and set its class to MyCell and add your components. Finally, connect the IBOutlets to the components. Note that we did not set the File's Owner in IB.

其他方法提倡设置文件的所有者,并警告内存泄漏,如果Xib没有通过额外的工厂类加载。我测试了上面的仪器/泄漏和没有看到内存泄漏。

Other methods advocate setting the File's Owner and warn of memory leaks if the Xib is not loaded via an additional factory class. I tested the above under Instruments/Leaks and saw no memory leaks.

那么,从Xibs加载单元格的规范方法是什么?我们设置文件的所有者吗?我们需要一个工厂吗?如果是这样,工厂的代码是什么样子?如果有多个解决方案,让我们澄清每个人的利弊...

So what's the canonical way to load cells from Xibs? Do we set File's Owner? Do we need a factory? If so, what's the code for the factory look like? If there are multiple solutions, let's clarify the pros and cons of each of them...

推荐答案

建议原始作者状态由IB工程师

查看实际的职位了解更多详情。我更喜欢方法#2,因为它似乎更简单。

See the actual post for more details. I prefer method #2 as it seems simpler.

方法1:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];
    if (cell == nil) {
        // Create a temporary UIViewController to instantiate the custom cell.
        UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"BDCustomCell" bundle:nil];
        // Grab a pointer to the custom cell.
        cell = (BDCustomCell *)temporaryController.view;
        [[cell retain] autorelease];
        // Release the temporary UIViewController.
        [temporaryController release];
    }

    return cell;
}

方法2: b
$ b

Method #2:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BDCustomCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
    }

    return cell;
}

更新(2014年):
方法#2仍然有效,但是没有文档了。它以前是在

Update (2014): Method #2 is still valid but there is no documentation for it anymore. It used to be in the official docs but is now removed in favor of storyboards.

我在Github上发布了一个工作示例:

I posted a working example on Github:
https://github.com/bentford/NibTableCellExample

这篇关于如何从Xib文件加载自定义UITableViewCells?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆