簡體   English   中英

無法為UITableViewCell設置文本

[英]Cant set text for UITableViewCell

我有UIViewControllertableView通過委托連接。 首次加載視圖時,它將使用AFNetworking啟動加載數據。 加載完成后,它將發送通知以重新加載數據。 但是,我遇到了一個非常奇怪的錯誤。 當我強制重新加載tableView時,由於某種原因,即使我有實際數據,我也無法為其標簽設置文本。 這是情節提要:

在此處輸入圖片說明

並且有實際的代碼。 當我們收到通知時(在我們從服務器獲取數據並用新數據填充數組之后),將調用以下代碼:

#pragma mark - Notification Center

-(void)loadingFinished: (NSNotification*) notification{

    NSLog(@"Recieved notify");

    /* Перезагружаем таблицу */

    isDataLoaded = YES;

    NSLog(@" find 2 %@", [[self.detailDataArray objectAtIndex:1]valueForKey:@"title"]);
    NSLog(@" find 3 %@", [[self.detailDataArray objectAtIndex:2]valueForKey:@"title"]);


    [self.spotlightTableView reloadData];

}

在CellForRow ...中:

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

    NSLog(@"Called once? ");

    // Инициализация ячейки


    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    if (!isDataLoaded){
        NSLog(@"called no bool");
   cell.titleLabel.text = @"No data";
        cell.dateLabel.text = @"No date";
    }   else {
        NSLog(@"called with b00l");
        [cell.myImageView setImageWithURL:[NSURL URLWithString:[[self.detailDataArray objectAtIndex:indexPath.row +1]valueForKey:@"standardImage"]]];
        cell.titleLabel.text = [[self.detailDataArray objectAtIndex:indexPath.row +1]valueForKey:@"title"];
        cell.dateLabel.text = [[self.detailDataArray objectAtIndex:indexPath.row +1]valueForKey:@"created_at"];

    }

    NSLog(@"title - %@", [[self.detailDataArray objectAtIndex:indexPath.row +1]valueForKey:@"title"]);
    NSLog(@"created_at0 - %@", [[self.detailDataArray objectAtIndex:indexPath.row +1]valueForKey:@"created_at"]);


    return cell;
}

而且有一件奇怪的事:在表格視圖中,我可以看到已加載的圖像(在imageView中),但是我只能看到文本

 cell.titleLabel.text = @"No data";
 cell.dateLabel.text = @"No date";

在此處輸入圖片說明

因此,在執行isLoaded檢查后,在else語句中傳遞的代碼塊被執行了,但是為什么我看不到文本? 我什至嘗試用假文本填充它,但仍然只能看到第一個輸出(未加載數據時)。

注意:MyCell是我也用於另一個控制器(在StoryBoard中具有相同標識符)的自定義類。

NSLog輸出為:

2016-01-25 14:57:01.894 MedApp[6056:159310] Recieved notify
2016-01-25 14:57:01.894 MedApp[6056:159310]  find 2 quis turpis vitae
2016-01-25 14:57:01.894 MedApp[6056:159310]  find 3 quis turpis vitae
2016-01-25 14:57:01.895 MedApp[6056:159310] Called once? 
2016-01-25 14:57:01.895 MedApp[6056:159310] called with b00l
2016-01-25 14:57:01.896 MedApp[6056:159310] title - quis turpis vitae
2016-01-25 14:57:01.896 MedApp[6056:159310] created_at0 - 2015-12-19 21:00:00
2016-01-25 14:57:01.897 MedApp[6056:159310] Called once? 
2016-01-25 14:57:01.897 MedApp[6056:159310] called with b00l
2016-01-25 14:57:01.897 MedApp[6056:159310] title - quis turpis vitae
2016-01-25 14:57:01.898 MedApp[6056:159310] created_at0 - 2015-12-19 21:00:00

謝謝您的幫助,問題出在我的自定義課上。 當我嘗試避免重用現有圖像的錯誤時(首先,將單元格先加載部分舊圖像,然后再加載新圖像),我在自定義單元格類實現中編寫了以下內容:

-(void)prepareForReuse{

    // Для избежания подгрузки неверных изображений, когда таблица пытается загрузить ячейки которые уже были использованны.

    self.dateLabel = nil;
    self.titleLabel = nil;
    [self.myImageView cancelImageRequestOperation];
    self.myImageView.image = nil;
}

當我刪除

self.dateLabel = nil;
self.titleLabel = nil;

一切都很好。 謝謝!

暫無
暫無

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

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