0

我有一个UITableViewxib.

  1. 设置了委托和数据源出口
  2. numberOfSectionsInTableView在那儿
  3. numberOfRowsInSection在那儿
  4. cellForRowAtIndexPath被调用

我可以设置单元格的背景颜色,它工作正常。但是,设置 的textLabel不起作用UITableViewCell

这是我的代码:

...
#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];
}
...

我的设置中唯一不同的是,包含此 UITableView 的 UIViewController 正在作为 childViewController 添加到父 UIViewController。这让我一开始相信这是问题所在,但由于设置单元格的背景颜色可以正常工作,我不相信这是一些 UIView 覆盖它的问题。


这是一些屏幕:

基本画面: 在此处输入图像描述


将背景颜色设置为蓝色时的屏幕: 在此处输入图像描述


编辑

我添加了一些日志语句,这让事情变得更加混乱:

- (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>>
4

1 回答 1

0

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

NSLog(@"%@", [property.descriptionShort class]);
于 2013-11-06T16:39:38.597 回答