有没有办法加载原型单元以及故事板中定义的任何 IBOutlet 连接?
更新
我想对单元格进行单元测试(该母校的 UICollectionViewCell),因此想在 UIViewController 上下文之外加载它。
实际上,就像您可以从 nib 加载自定义视图一样,指定其文件的所有者并设置其 IBOutlet(s)。
有没有办法加载原型单元以及故事板中定义的任何 IBOutlet 连接?
更新
我想对单元格进行单元测试(该母校的 UICollectionViewCell),因此想在 UIViewController 上下文之外加载它。
实际上,就像您可以从 nib 加载自定义视图一样,指定其文件的所有者并设置其 IBOutlet(s)。
我还没有通过单元测试尝试过这个,但是你可以很容易地把你的自定义UITableViewCell
放到一个单独的 nib 中。
要在视图控制器中使用它,您需要将单元格注册到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];
为了您的测试目的,您应该能够使用:
ABCNameOfYourNibCell *testCell =
[[ABCNameOfYourNibCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:nil];
如果您需要测试重用行为,您应该在此处设置重用标识符并调用prepareForReuse
单元格。
通常你创建一个UITableViewController
或一个UITableView
。比你还应该创建一个UITableViewCell
类。创建UITableViewCell
类后,转到`UIStoryboard,选择单元格:
然后在UITableViewCell
里面设置类Identity Inspector
:
现在添加元素UITableViewCell
并将它们与您的类连接起来
现在在CellIdentifier
里面添加Attributes Inspector
:
没有得到你UITableViewController
或UIViewController
你有UITableViewDelegate
方法的地方并像这样调用你的单元格(不要忘记你顶部的#import
类:UITableViewCell
ViewController
- (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;
}