簡體   English   中英

UITableView不顯示UITableViewCell文本

[英]UITableView not showing UITableViewCell text

我有一個UITableView在創建xib

  1. 設置了代理和數據源出口
  2. 還有numberOfSectionsInTableView
  3. 這里有numberOfRowsInSection
  4. cellForRowAtIndexPath被調用

我可以設置單元格的背景顏色,並且效果很好。 但是,設置UITableViewCelltextLabel不起作用。

這是我的代碼:

...
#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>>

檢查property.descriptionShort是否存儲NSString對象,還檢查編譯警告。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM