[英]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。
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
并且像这样调用你的单元格(不要忘记#import
在ViewController
顶部的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.