簡體   English   中英

NSURLConnection委托和填充表視圖

[英]NSURLConnection delegate and populating table view

我必須致電NSURLConnection並具有以下委托:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

    NSString *responseString = [[NSString alloc] initWithData:responseData  
        encoding:NSUTF8StringEncoding];
    [responseData release];

    results = [NSArray array];
    results = [responseString JSONValue];
        NSLog(@"Number of rows: %d", [results count]);
}

//table view number of rows    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        //return [_userInfo.friendsInfo count];
        NSLog(@"Number of rows: %d", [results count]);
        return [results count];
    }

如您所見,我嘗試打印出數組中的條目數,當在NSURLConnection的委托中獲得5時,在tableView numberOfRowsInSection中獲得0。 我想使用從NSURLConnection獲得的數據填充表。 我怎樣才能做到這一點?

我看到兩個潛在的問題:

  1. 您缺少保留。 該行“結果= [NSArray數組];” 是沒有意義的,您應該將JSONValue保留為“ results = [[[responseString JSONValue] keep];”。 (記住要釋放!)
  2. 另外,獲取數據后是否要重新加載tableView? 您應該執行類似[table reloadData]的操作; 或[self.tableView reloadData]; 取決於您的班級結構。

您是否保留結果指向的數組? 我認為您需要調用setResults (如果已合成)或顯式保留。

暫無
暫無

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

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