2012-03-21 66 views
1

當我上下滾動時,UITableView只顯示來自JSON數組的數據。當我加載表格視圖時,它會顯示單元格,但它們是空白的,當我上下滾動時,它會開始顯示數據。當我向上和向下滾動時,UITableView只顯示來自JSON的數據

如何在不滾動的情況下顯示包含數據的單元格?

- (void)requestFinished:(ASIHTTPRequest *)request 
{  
    if (request.responseStatusCode == 400) { 
     NSLog(@"Code already used"); 

    } else if (request.responseStatusCode == 403) { 
     NSLog(@"Code already used"); 

    } else if (request.responseStatusCode == 200) { 
     NSLog(@"%@",[request responseString]); 
     NSString *response = [request responseString]; 
     const char *convert = [response UTF8String]; 
     NSString *responseString = [NSString stringWithUTF8String:convert]; 
     responseArray = [responseString JSONValue]; 
     Nrows = [responseArray count]; 

    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSDictionary *dict = [responseArray objectAtIndex:indexPath.row]; 
    cell.detailTextLabel.text = [dict objectForKey:@"allfeeds"]; 
    cell.textLabel.adjustsFontSizeToFitWidth = YES; 
    cell.textLabel.font = [UIFont systemFontOfSize:12]; 
    cell.textLabel.minimumFontSize = 10; 
    cell.textLabel.numberOfLines = 4; 
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
    cell.textLabel.text = [dict objectForKey:@"allfeeds2"]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    return cell; 
} 
+0

哪裏是你的代碼? – Bourne 2012-03-21 16:28:04

回答

1

而是開始異步請求(非阻塞)

[request startAsynchronously]; 

嘗試同步做(阻塞)

[request startSynchronously]; 
+0

其神奇完成! :)非常感謝它的工作..現在[requeststartSynchronously];已經制定出 – 2012-03-22 14:09:31

+0

我很高興它幫助:) – Myxtic 2012-03-22 14:12:46

0

我也有類似的問題,並得到了我在蘋果的開發者論壇回答前一陣子,這解決了我的問題:

從你所描述的東西,我會想這個問題是更有可能位於cellForRowAtIndexPath中,或者在您執行單元的實際配置時發生的位置,而不是您發佈的代碼中的位置。當你的dequeueReusableCellWithIdentifier返回一個現有的單元格時,像你這樣的問題可能會發生,但是你不會完全重置它的現有狀態。

完整的問題和答案都可以在這裏找到(需要登錄):https://devforums.apple.com/message/502483#502483

+0

你能告訴我我如何重置它的狀態? – 2012-03-21 19:05:10

+0

您必須更改單元格中的每一位信息。假設你有一個標題,一個副標題和一個'accessoryType.UITableViewCellAccessoryCheckmark',因此對於你繪製的每個單元格,你必須修改這三個單元格,即使其中一個單元格對於所有單元格都是相同的,你只需重寫它。 – 2012-03-22 09:31:12

+0

但在我的情況下,我從json獲取NSArray,並且每個單元格中都有兩個文本字段,如文本標籤和詳細文本。 – 2012-03-22 13:21:28

0

這些問題通常是由UITableView的緩存造成的,即從隊列具有相同CellIdentifier他提供緩存的UITableViewCell(你的情況 - @「細胞」)。

嘗試爲每個表格的每個單元格創建一個單元格標識符。您可以使用NSIndexPath PARAM通過以下方式創建此方差:

NSString* cellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row]; 

那當然,假設下你不改變你的細胞加載數據後的內容。

+0

我做了,但沒有運氣,當我加載表視圖顯示空白,並作爲滾動它開始顯示我的價值觀,如果我再次上去它開始顯示我其餘的人,我加載它異步。請推薦一些東西 – 2012-03-21 20:58:27

+0

當我向下滾動顯示數值的statrd時,它會加載空白。 – 2012-03-21 20:59:19

2

當您完成您的請求,您設置的陣列,請確保調用[self.tableView reloadData];

性能和動畫的目的,表視圖不刷新自己的數據源改變時。

相關問題