Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - UILabel loading, but no text displaying

I have implemented my own UITableViewCell and created a .xib for it. I have connected all the labels using a strong IBOutlet and initialized the UILabels in the awakeFromNib() method. However, whenever I run the iOS Simulator, I run into an issue where the UILabel is (null) in NSLog.

I am wondering if it has to do with how I am loading in the text for the UILabel. I have tried to create a shortened version of the project below that outlines the issue I'm running into.

I would also like to note that I can click on the actual rows, but that there is still no text displaying.

My code:

ToDoCell.h

#import <UIKit/UIKit.h>

@interface ToDoCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *minutesLeft;
@property (strong, nonatomic) IBOutlet UILabel *hoursLeft;
@property (strong, nonatomic) IBOutlet UILabel *daysLeft;
@property (strong, nonatomic) IBOutlet UILabel *taskLabel;

@end

ToDoCell.m

#import "ToDoCell.h"

@implementation ToDoCell

@end

ToDoViewController.m

#import "ToDoViewController.h"
#import "ToDoCell.h"

@interface ToDoViewController ()

@property NSMutableArray *toDoItems;

@end

@implementation ToDoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.toDoItems = [[NSMutableArray alloc] init];
    [self loadInitialData];
}

- (void)loadInitialData {
    NSString *item1 = @"Testing";
    [self.toDoItems addObject:item1];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ToDoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ToDoCell" forIndexPath:indexPath];

    cell.taskLabel.text = @"Testing";
    NSLog(@"Fudge Monkeys: %@", cell.taskLabel.text);

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

@end
like image 990
Zhouster Avatar asked Jan 08 '23 17:01

Zhouster


1 Answers

Issue is you are creating new objects for labels but then you are not adding them to superview but calling [self addSubview:label] ssigning them to properties does not add them to super view.

But this is totally unnecessary you should not init them in awakeNib. , and one thing more I will say about your code now you don't need to use @synthesis anymore as well. Any particular reason you are making IBOutlets strong?

Update you need to register nib with tableView first add following lines in viewDidLoad

[tableView registerNib:[UINib nibWithNibName:@"ToDoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ToDoCell"];

You can read more about this here

like image 70
Adnan Aftab Avatar answered Jan 12 '23 00:01

Adnan Aftab