8
votes

I looked around and there are many examples on how to do this, but I am still not getting it.

I have a list where I am listing journal entries which I want to display a title, date created and few other things. I want to use an XIB to design a cell, so UX designer can modify it and pixel push it.

So, this is how i setup the xib: enter image description here

Then I made a class that this xib cell points to and I added two properties to it:

@property (atomic, weak) IBOutlet UILabel* titleLabel;
@property (atomic, weak) IBOutlet UILabel* dateLabel;

I want to be able to set these when I create these cells.

In the view that has the list table I do this: In -(void) viewDidLoad

[self.tableView registerNib:[UINib nibWithNibName:@"DreamListTableViewCell" bundle:nil] forCellReuseIdentifier:@"DreamListCell"];

And in - (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I do this:

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

The problem is that the app crashes if I setup IBOutlets in the cell xib, because I guess the owner is really nil when it is initialized, but I am actually not sure. How could I modify those two properties if I can't connect them in interface builder? What should I do differently?

Also, I am not using storyboards. I am using this as a guide: Chapter 21. Table Views and Collection Views

I apologize for sort of duplicate question, I am just not getting it. Halp!

Edit: Outlet screenshot: enter image description here

Edit #2: If I remove outlet connections in the cell xib i see this: enter image description here

1
Did you bind your custom class to the cell in IB? You also need to specify that custom class and not UITableViewCell in the cellForRowAtIndexPath - sha
I cast 'cell' to my custom cell class later on, but it crashes before that code is executed. I set file owner to and the root view in the xib to point to my custom cell class :( - SpaceBear
Where does it crash exactly and what's the exception says? - sha
My answer came a bit too late, sorry about that @pbasdf - Andris Zalitis
@AndrisZalitis no problem. Just glad SpaceBear got a solution. - pbasdf

1 Answers

7
votes

Don't use File's Owner to set the relationships to IBOutlets. Change the class of the cell and use that instead (might be tricky to make the connections on some Xcode versions, but is doable).