Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My custom UITableViewCell table cell shows up as a blank list of items

Basically I have a custom UITableViewCell which I made in my nib file. Within that I have some labels and whatnot that I want to be able to edit programatically. I called my nib file "post" and I made its file owner "post" and I set its class to "post" under file owner as well. I linked all my labels within my nib to this class:

post.h:

#import <Foundation/Foundation.h>

@interface post : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *title;
@property (nonatomic, weak) IBOutlet UILabel *authorComments;
@property (nonatomic, weak) IBOutlet UILabel *time;

@end

post.m:

#import "post.h"

@implementation post

    @synthesize title = _title;
    @synthesize authorComments = _authorComments;
    @synthesize time = _time;

@end

An image of my nib file:

enter image description here

So now everything in my nib file is linked with my post class except for the actual cell itself because I was told I had to link that with a "view" but I'm not sure how (I tried linking it to backgroundView but this did not fix my problem). I've also tried giving the actual cell object the class post but it does not fix my problem.

Then in my controller I have the following code:

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

    // this is the identifier that I gave the table cell within my nib
    static NSString *simpleTableIdentifier = @"post";

    // grabs me a cell to use
    post *cell = (post *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[post alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    // sets the text on one of the labels within my cell to something else
    cell.title.text = @"this is a test";

    // returns my customized cell
    return cell;

}

However when I run my app I just get this:

enter image description here

I know that the problem I'm having has to do with my custom cell, because I had it working previously, when I set the file owner on my nib to my controller, instead of attempting what I'm doing now which is trying to make a subclass of UITableViewCell. All I want to do is to be able to create a custom cell with multiple different labels on it, and then be able to edit the text on the cell programatically.

If it helps, here are all my source files and my nib file:

https://dl.dropboxusercontent.com/u/7822837/source.zip

like image 389
David Zorychta Avatar asked Dec 06 '22 08:12

David Zorychta


2 Answers

You have to registerNib:forCellReuseIdentifier: in your UITableView, something like this:

- (void)viewDidLoad {
   [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"post" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"post"];
}

Swift:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.registerNib(UINib(nibName: "post", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "post")
}
like image 51
juanjo Avatar answered Jan 19 '23 00:01

juanjo


You have to do two things

In xib file

DisConnect all outlets of file owner and connect the outlets to your custom cell object

enter image description here

In code

Load the cell from nib

if (cell == nil) {

    //cell = [[post alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    cell = [[[NSBundle mainBundle]loadNibNamed:@"post" owner:self options:nil]objectAtIndex:0];
}
like image 36
Anil Varghese Avatar answered Jan 19 '23 01:01

Anil Varghese