18

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

Update

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

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.

1
  • 1
    you should connect the IBOutlets to the interface of a custom subclass of UITableViewCell.
    – lootsch
    Commented Mar 7, 2014 at 18:08

2 Answers 2

14

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.

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

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"];

Then use the cell like this in 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.

1
  • 1
    I'll take it that the answer is 'NO' and the only way is to declare it to a standalone nib, right?
    – qnoid
    Commented Mar 10, 2014 at 10:16
10

Normally you crete an UITableViewController or a UITableView. Than you should also create a UITableViewCell class. After creating the UITableViewCell class, go to the `UIStoryboard, select the cell :

enter image description here

Then set the UITableViewCell class inside the Identity Inspector:

enter image description here

Now add elements to the UITableViewCell and connect them with your class

enter image description here

Now add the CellIdentifier inside the Attributes Inspector:

enter image description here

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:

- (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;
}
3
  • Can you please be more specific? Do you mean if the prototype cell is connected as an IBOutlet to the view controller?
    – qnoid
    Commented Mar 7, 2014 at 18:39
  • Sorry for the last question, thought you want to load the cell from another UIViewController, this should be what your looking for
    – Alex Cio
    Commented Mar 7, 2014 at 19:58
  • 7
    Irrelevant answer as question is how to load prototype cell created in storyboard in the separate test class.
    – valdyr
    Commented Sep 14, 2017 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.