简体   繁体   English

如何从故事板加载原型单元?

[英]How do you load a prototype cell from a storyboard?

Is there a way to load a prototype cell, along with any IBOutlet connections as defined within a storyboard? 有没有办法加载原型单元格,以及故事板中定义的任何IBOutlet连接?

Update 更新

I want to unit test the cell (a UICollectionViewCell for that mater), hence would like to load it outside of a UIViewController context. 我想对单元格(单元的UICollectionViewCell)进行单元测试,因此希望将它加载到UIViewController上下文之外。

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. 实际上,就像您可以从笔尖加载自定义视图一样,指定其文件的所有者并设置其IBOutlet。

Edit: 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. 编辑:据我所知,除了你创建的ViewController之外,不可能在Storyboard中使用原型UITableViewCells。

I haven't tried this with unit tests yet but you can easily put your custom UITableViewCell into a separate nib. 我还没有尝试使用单元测试,但您可以轻松地将自定义UITableViewCell放入单独的笔尖中。

For using it in your view controllers you need to register the cell with your tableViews. 要在视图控制器中使用它,您需要使用tableViews 注册单元格

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

Then use the cell like this in cellForRowAtIndexPath: 然后在cellForRowAtIndexPath:使用这样的单元格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];

If you need to test reuse-behaviour, you should set a reuseIdentifier here and call prepareForReuse on the cell. 如果需要测试重用行为,则应在此处设置reuseIdentifier并在单元上调用prepareForReuse

Normally you crete an UITableViewController or a UITableView . 通常你会创建一个UITableViewController或一个UITableView Than you should also create a UITableViewCell class. 比你还应该创建一个UITableViewCell类。 After creating the UITableViewCell class, go to the `UIStoryboard, select the cell : 创建UITableViewCell类之后,转到`UIStoryboard,选择单元格:

在此输入图像描述

Then set the UITableViewCell class inside the Identity Inspector : 然后在Identity Inspector设置UITableViewCell类:

在此输入图像描述

Now add elements to the UITableViewCell and connect them with your class 现在将元素添加到UITableViewCell并将它们与您的类连接

在此输入图像描述

Now add the CellIdentifier inside the Attributes Inspector : 现在在Attributes Inspector添加CellIdentifier

在此输入图像描述

No got to your UITableViewController or the UIViewController where you have the UITableViewDelegate methods and call your cell like this (don't forget to #import the UITableViewCell class at the top of your ViewController : 没有你的UITableViewController或你有UITableViewDelegate方法的UIViewController并且像这样调用你的单元格(不要忘记#importViewController顶部的UITableViewCell类:

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

    static NSString *CellIdentifier = @"MyIdentifier";
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                                   forIndexPath:indexPath];

    [cell.label setText:[NSString stringWithFormat:@"My Cell at Row %ld", 
                         (long)indexPath.row]];      
    return cell;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何连接故事板中的原型单元? - How to connect a prototype cell in a storyboard? 情节提要中的原型单元未创建UILabels等 - Prototype cell from storyboard not creating UILabels, etc 您如何以编程方式对作为情节提要原型创建的UITableViewCell进行初始设置? - How do you programmatically do initial setup to a UITableViewCell created as a storyboard prototype? 如何将情节提要中的原型单元连接到以编程方式创建的单元? - How to connect a prototype cell in storyboard to a cell created programatically? 你如何从一个故事板转到另一个故事板? 不能在故事板中查看 - How do you go from one storyboard to another? not view to view within a storyboard 在情节提要的原型单元中更改UIButton状态 - Change UIButton state in Prototype Cell in Storyboard 如何从故事板返回自定义单元格高度? - How to return custom cell height from storyboard? 如何从故事板或 xib 加载 iCarousel 视图? - How to load iCarousel view from storyboard or xib? 在故事板中,如何制作用于多个控制器的自定义单元格? - In a storyboard, how do I make a custom cell for use with multiple controllers? 访问情节提要中的UITableViewController中定义的原型单元,以在代码中实例化该类 - Accessing prototype cell defined in UITableViewController in storyboard for instantiation of that class in code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM