如何从情节提要中加载原型单元

如何从情节提要中加载原型单元

本文介绍了如何从情节提要中加载原型单元?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以加载原型单元以及情节提要中定义的任何IBOutlet连接?

Is there a way to load a prototype cell, along with any IBOutlet connections as defined within a storyboard?

更新

我想对单元格(该母体的UICollectionViewCell)进行单元测试,因此想将其加载到UIViewController上下文之外.

I want to unit test the cell (a UICollectionViewCell for that mater), hence would like to load it outside of a UIViewController context.

有效地,就像您可以从笔尖加载自定义视图一样,指定其文件的所有者并设置其IBOutlet.

Effectively, in the same way that you can load a custom view from a nib, specifying its file's owner and have its IBOutlet(s) set.

推荐答案

据我所知,除了您在其中创建的ViewController之外,无法从Storyboard使用原型UITableViewCells.

我还没有在单元测试中尝试过这种方法,但是您可以轻松地将自定义UITableViewCell放入单独的笔尖中.

As far as I know, it's not possible to use prototype UITableViewCells from a Storyboard anywhere other than the ViewController you created it in.

I haven't tried this with unit tests yet but you can easily put your custom UITableViewCell into a separate nib.

要在视图控制器中使用它,您需要在tableViews中注册该单元格.

For using it in your view controllers you need to register the cell with your tableViews.

UINib *nib = [UINib nibWithNibName:@"ABCNameOfYourNibCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"myCustomCell"];

然后在cellForRowAtIndexPath:

static NSString *CellIdentifier = @"myCustomCell";

ABCNameOfYourNibCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

出于测试目的,您应该可以使用:

For your testing purposes you should be able to go with:

ABCNameOfYourNibCell *testCell =
[[ABCNameOfYourNibCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:nil];

如果需要测试重用行为,则应在此处设置一个reuseIdentifier,然后在单元格上调用prepareForReuse.

If you need to test reuse-behaviour, you should set a reuseIdentifier here and call prepareForReuse on the cell.

这篇关于如何从情节提要中加载原型单元?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 06:36