UILabel that is part of custom UITableViewCell text not updating in cellForRowAtIndexPath

时间:2019-01-09 22:20:50

标签: ios objective-c xcode uitableview uilabel

The text for my UILabels will not update. Here are the UITableViewController delegate functions:

#import "PoolsTableViewController.h"

@interface PoolsTableViewController ()

@end

@implementation PoolsTableViewController

-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    self.items = [[NSMutableArray alloc]init];
    [self.items addObject:@"Hello"];
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"PoolsTableViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.items count];
}

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

    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1" forIndexPath:indexPath];
    NSLog(@"indexPath.row: %li, %@", (long)indexPath.row, [self.items objectAtIndex:indexPath.row]);

    PoolsTableViewCell *cell = (PoolsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.cellLeftViewLabel.text = [self.items objectAtIndex:indexPath.row];
    cell.cellRightViewLabel.text = [self.items objectAtIndex:indexPath.row];

    return cell;
}

@end

In cellForRowAtIndexPath I try and change the text to the string that is contained in self.items. The log output is:

indexPath.row: 0, Hello

So I know that it is actually there before I try to change the UILabel text. This is what my custom UITableViewCell looks like:

PoolsTableViewCell.h

#import <UIKit/UIKit.h>

@interface PoolsTableViewCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *cellLeftViewLabel;
@property (strong, nonatomic) IBOutlet UILabel *cellRightViewLabel;
@property (strong, nonatomic) IBOutlet UIImageView *cellLeftImageView;
@property (strong, nonatomic) IBOutlet UIImageView *cellRightImageView;

@end

PoolsTableViewCell.m

#import "PoolsTableViewCell.h"

@implementation PoolsTableViewCell
@synthesize cellLeftViewLabel;
@synthesize cellRightViewLabel;
@synthesize cellLeftImageView;
@synthesize cellRightImageView;

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    cellLeftViewLabel.numberOfLines = 0;
    cellRightViewLabel.numberOfLines = 0;
    cellLeftViewLabel.textColor = [UIColor blackColor];
    cellRightViewLabel.textColor = [UIColor blackColor];
}

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

    // Configure the view for the selected state
}

@end

What am I doing wrong?

UPDATE

Everything is linked correctly in the Interface Builder I think:

enter image description here

UPDATE

Screen after breakpoint and breakpoint settings:

enter image description here

0 个答案:

没有答案