2012-10-23 55 views
0

我在嘗試使用網絡請求的結果填充UITableView時遇到問題。看起來我的代碼是正常的,因爲當我的網絡很快時它可以正常工作,但是,如果沒有,仍會執行功能 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath-,這會導致訪問錯誤。我想這是因爲上述功能試圖利用的數組沒有被填充。這使我想到我的問題:無論如何,我可以延遲UITableView委託方法來避免這種情況發生?UITableViewController在網絡請求完成之前執行delate函數

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"AlbumsCell"; 
//UITableViewCell *basicCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

AlbumsCell *cell = (AlbumsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (!cell) { 
    **// Here is where the Thread 1: EXC_BAD_ACCESS (code=2 address=0x8)** 
    cell = [[[AlbumsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

    Album *album = [_albums objectAtIndex:[indexPath row]]; 
    [cell setAlbum:album]; 

return cell; 
} 

回答

2

修改您的委託方法以處理「進行中」狀態的網絡請求。一旦你得到你的迴應,請在tableview上調用reloadData,它將用適當的數據重新載入表格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"AlbumsCell"; 
//UITableViewCell *basicCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    AlbumsCell *cell = (AlbumsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!cell) { 
     **// Here is where the Thread 1: EXC_BAD_ACCESS (code=2 address=0x8)** 
     cell = [[[AlbumsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    if ((_albums) && ([_albums count] > [indexPath row])) { 
     Album *album = [_albums objectAtIndex:[indexPath row]]; 
     [cell setAlbum:album]; 
    } else { 
    //show some loading message in the cell 
    } 

    return cell; 
    } 
+0

我其實曾嘗試過類似的東西。我剛剛嘗試了你的代碼,當我運行我的應用程序時,它最多次給了我if((_albums)&&([_albums count]> [indexPath row]))的EXC_BAD_ACCESS。我現在完全失去了。爲什麼會發生?起初我以爲這是因爲網絡執行,但現在我不太確定,因爲這個錯誤不是我試圖訪問數組中的任何索引的結果。 – davetw12

+0

它是否在if條件內?究竟是什麼時候崩潰?什麼是完整的崩潰消息? – iDev

+0

條件的實際聲明是它崩潰的位置(如果((_albums)&&([_albums count]> [indexPath row])){)。我以爲這也許是因爲我沒有正確保留我的陣列,但如果是這樣的話,爲什麼這個崩潰不會發生一切? – davetw12

相關問題