swe_mattias
swe_mattias

Reputation: 641

Custom UITableViewCell doesn't show anything

I have done my homework, I thought I had everything under control. When I run my project all goes fine, but the UITextView in the custom UITableViewCells are empty.

Here is the code that matters:

[self.tableView registerClass:[NewsCell class]forCellReuseIdentifier:@"NewsCell"];

And then:

NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsCell" forIndexPath:indexPath];

NSDictionary *newsObject = [_newsArray objectAtIndex:indexPath.row];
NSString *title = [newsObject objectForKey:@"newsTitle"];

NSLog(@"title: %@", title); // This puts out the correct stuff

[cell.newsTeaser setText:title];

But the newTeaser UITextView shows up empty. Why is this and what error have I made? I should add that this my first time making custom cells in iOS 7...

Upvotes: 0

Views: 1275

Answers (2)

AVog
AVog

Reputation: 321

If you are fetching data from an API endpoint, it's possible that your call block is happening on the main/UI thread, so your UI is blocked while waiting for the call block to finish fetching the data.

If that's the case, you need to use dispatch_async inside your block method like this:

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    }); 

Upvotes: 0

Michael Ochs
Michael Ochs

Reputation: 2870

In my experience this often happens because you are implementing the wrong init method. Keep in mind that the designated initialiser for a UITableViewCell is initWithStyle:reuseIdentifier:. If you are adding a custom view hierarchy and you don't do this lazily or inside this initialiser, your views will not be created.

In the case your cells should also be compatible with interface builder, you should also implement awakeFromNib.

//EDIT: However if you are using interface builder as stated in the comments, you are using the wrong register method.registerClass:forCellReuseIdentifier: is only for creation of classes through code. If there is a xib file with your custom cell layout, you need to register that xib file with registerNib:forCellReuseIdentifier:. And if you are using a storyboard, you don't have to register the cell at all. You only need to specify the cell's reuseIdentifier in the storyboard file.

Upvotes: 3

Related Questions