Galip
Galip

Reputation: 5455

Label in CustomCell stays nil

I created a custom UITableViewCell with an UILabel in it.

In the cellForRowAtIndexPath method I initialise the custom cell and give the UILabel a value.

While the custom cell is loaded (the heights of the cells are higher than default cells), I can't seem to give the UILabel a value.

SBDownloadCell.h

#import <UIKit/UIKit.h>

@interface SBDownloadCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UIProgressView *progressbar;
@property (weak, nonatomic) IBOutlet UILabel *details;

- (IBAction)pause:(id)sender;

@end

SBDownloadCell.m

#import "SBDownloadCell.h"

@implementation SBDownloadCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)pause:(id)sender {
}
@end

SBViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SBDownload";

    SBDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[SBDownloadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    SBDownload *dwnld = [[SBDownload alloc] init];
    dwnld = [SBdownloads objectAtIndex:indexPath.row];

    //cell.title.text = [dwnld name];
    cell.title.text = @"Test";
    cell.progressbar.progress = [dwnld percentDone];
    cell.details.text = [NSString stringWithFormat:@"%f MB of %f MB completed, %@", [dwnld completed], [dwnld length], [dwnld eta]];

    return cell;
}

Storyboard

enter image description here

I break just after cell.title.text = @"Test"; and still this is what I see:

enter image description here

What could it be?

note: i use Xcode DP-5 with iOS7

Upvotes: 2

Views: 4925

Answers (4)

Diva
Diva

Reputation: 129

I encounter same problem but my identifier is correct for creating custom cell and show data in IBOutlets custom lable, but after cell allocation lables values are null. So for this I need to add method of tableview for showing lables values

- (void)tableView:(UITableView *)tableView willDisplayCell:(DropViewTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  {
    cell.lblHSNCode.text = objClass.HSNCode;
  }

Upvotes: 0

haiwuxing
haiwuxing

Reputation: 1162

If you custom your prototype cells in Storyboard, you need to present your TableViewController with segue in storyboard, or if you you need to present the TableViewController by programming, you need to use instantiateViewControllerWithIdentifier: method,

- (IBAction)showTableView:(id)sender
{
    UIStoryboard *storybaord = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
    LJTableViewController *vc = (LJTableViewController*)[storybaord
                                                         instantiateViewControllerWithIdentifier:@"TableViewControllerID"];
    [self presentViewController:vc animated:YES completion:nil];
}

if you initiate TableViewController with [TableViewContrller new], this will let you get an nil label in tableView:cellForRowAtIndexPath mehtod.

Upvotes: 0

L&#233;o Natan
L&#233;o Natan

Reputation: 57040

I see your properties are marked with IBOutlet, which means you have an Interface Builder file (either xib or storyboard) with your table view cell. Make sure to give the correct cell identifier in Interface Builder to the prototype cell in your table view. You should not be calling initWithStyle:. If dequeueReusableCellWithIdentifier: returns nil when using a UITableViewController and storyboards, this means incorrect setup, as dequeueReusableCellWithIdentifier: should always return a cell (it creates the new one if it has to).

To elaborate a bit further, when using xibs or storyboards, a table view cell's initWithStyle: will never be called. When a nib is loaded, the correct init method is initWithCoder:.

The problem is in static NSString *simpleTableIdentifier = @"SBDownload";. In your storyboard, you have set up the identifier as SBDownloadCell.

Upvotes: 4

bachonk
bachonk

Reputation: 3954

You need to allocate your UILabels and UIProgressView. Right now you set properties for them, but in your -initWithStyle method you need to call things like

self.title = [[UILabel alloc] initWithFrame:etc...];

If you do this for each of your properties on SBDownloadCell, the labels should be allocated properly.

Upvotes: 0

Related Questions