简体   繁体   中英

UITableView not showing UITableViewCell text

I have a UITableView created in a xib .

  1. The delegate and datasource outlets are set
  2. numberOfSectionsInTableView is there
  3. numberOfRowsInSection is there
  4. cellForRowAtIndexPath is being called

I can set the background color of the cell and it works fine. However, setting the textLabel of the UITableViewCell does not work.

Here is my code:

...
#pragma mark -
#pragma mark - UITableView datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[[[PropertyManager sharedManager] fetchedResultsController] fetchedObjects] count];
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Property *property = [[[PropertyManager sharedManager] fetchedResultsController] objectAtIndexPath:indexPath];

    //This DOES prints to the console properly
    NSLog(@"%@", property.descriptionShort);

    [cell.textLabel setText:property.descriptionShort];
    [cell.textLabel setTextColor:[UIColor blackColor]];

    return cell;
}


#pragma mark -
#pragma mark - 
- (void)filterToUsersLocation {}

- (void)viewDidLoad
{
    [super viewDidLoad];

    /*
     I tried with a custom UITableViewCell that didn't work either
    [self.table registerClass:[PropertyCustomCell class] forCellReuseIdentifier:cellIdentifier];
    [self.table registerNib:[UINib nibWithNibName:@"PropertyCustomCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
     */
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.table reloadData];
}
...

The only thing that is different in my setup is that the UIViewController that contains this UITableView is being added as a childViewController to a parent UIViewController. Which lead me to believe that this was the problem at first, but since setting the background color of the cell works fine, I don't believe it to be some UIView covering it issue.


Here is some screens:

The base screen: 在此处输入图片说明


Screen when setting the background color to blue: 在此处输入图片说明


EDIT

I've added a few more log statements which is makes things even more confusing:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Property *property = [[[PropertyManager sharedManager] fetchedResultsController] objectAtIndexPath:indexPath];

    //This DOES prints to the console properly
    NSLog(@"%@", property.descriptionShort);
    //Prints out __NSCFString properly
    NSLog(@"%@", [property.descriptionShort class]);

    cell.backgroundColor = [UIColor blueColor];

    [cell.textLabel setText:property.descriptionShort];
    [cell.textLabel setTextColor:[UIColor blackColor]];

    //Prints out correctly
    NSLog(@"cellTextLabel = %@", cell.textLabel.text);

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    //Prints out correctly
    NSLog(@"cell textLabel = %@", cell.textLabel.text);
} 

<UITableView: 0x151c8c00; frame = (0 0; 320 568); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1469e340>; layer = <CALayer: 0x1469d9c0>; contentOffset: {0, 0}>
   | <UITableViewWrapperView: 0x1469eab0; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x1469eba0>>
   |    | <UITableViewCell: 0x1462b4e0; frame = (0 528; 320 44); text = '
      Grand 6 Bd Home on...'; autoresize = W; layer = <CALayer: 0x1462b670>>
   |    |    | <UITableViewCellScrollView: 0x1462bba0; frame = (0 0; 320 44); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1462b3b0>; layer = <CALayer: 0x1462bd70>; contentOffset: {0, 0}>
   |    |    |    | <UITableViewCellContentView: 0x1462bf20; frame = (0 0; 320 43.5); gestureRecognizers = <NSArray: 0x1462c5f0>; layer = <CALayer: 0x1462bf90>>
   |    |    |    |    | <UILabel: 0x1462c740; frame = (15 0; 290 43.5); text = '

    . . .
    . . .  
    . . .

   | <UIImageView: 0x1466dc10; frame = (0 564.5; 320 3.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x1466dca0>>
   | <UIImageView: 0x1469e030; frame = (316.5 561; 3.5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x1466dcd0>>

检查property.descriptionShort是否存储NSString对象,还检查编译警告。

NSLog(@"%@", [property.descriptionShort class]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM