2012-02-06 205 views
1

我試圖創建一個應用程序並排放置兩個UITableViews。左側列出文章類別,右側列出文章預覽(類似flipboard的搜索視圖)。並排重新加載數據UITableView

在左側桌面的didSelectRowAtIndexPath上,我應該下載文章並在右側的UITableView上顯示預覽。但是,我似乎無法完成這項工作。

我的假設是我在下載完成之前重新加載tableview上的數據。有什麼建議麼?

EDITED

這裏是我當前的代碼:

- (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] autorelease]; 
     if (tableView.tag == 1) 
     { 
      //if it's the left tableView (no problem here) 
      NSDictionary *catDic = [[Category categories] objectAtIndex:indexPath.row]; 
      cell.textLabel.text = [catDic valueForKey:@"name"]; 
      cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:[UIFont labelFontSize]]; 
     } 

     if (tableView.tag == 2) 
     { 
      //if it's the right tableView 
      ArticlePreview *articleView = [[ArticlePreview alloc] initFlexibleHeightRowForArticleInfo:[self.articleInfos objectAtIndex:indexPath.row]]; 
      //ArticlePreview is a custom class that create the articlePreview view, 
      //articleInfos is a variable that holds the articles in core data 
      [cell.contentView addSubview:articleView]; 
      [articleView release]; 
     } 
    } 

    return cell; 
} 

-(void) loadArticlePreview: (NSNumber *)_idx 
{ 
    [Category downloadArticlesforIndex:[_idx intValue]]; 

    AppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 
    NSManagedObjectContext *context = [delegate managedObjectContext]; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ArticleInfo" inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 
    NSError *error; 
    self.articleInfos = [context executeFetchRequest:fetchRequest error:&error]; 

    [fetchRequest release]; 

    [self.articlePreviewTableView reloadData]; 
    //articlePreviewTableView is the right table view identifier, hooked with IBOutlet and all 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView.tag == 1) //if it's the left table 
    { 
     [self performSelectorInBackground:@selector(loadArticlePreview:) withObject:[NSNumber numberWithInt:indexPath.row]]; 
    } 
} 

的問題是正確的tableview不刷新。我認爲這些方法可能是問題所在。

+0

你現在怎麼做,你跑什麼毛病?請儘可能具體,並在適用的情況下顯示代碼。 – sosborn 2012-02-06 02:59:02

回答

0

根據你的代碼,如果你將一個UITableViewCell出隊,它將使用舊的單元格,而不需要修改你需要的實際單元格。將其更改爲:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
}//after this, use the tableView tag to identify. 

您還正在向預覽表單元格添加內容視圖。我強烈建議您創建一個自定義UITableViewCell類,當你這樣做。我發現這是添加子視圖在單元格中工作的唯一方式,並且更容易用自定義類管理單元格。

我假設您正在使用ArticlePreview中的某種方法進行下載。下載完成後,您無需重新加載tableView。由於ArticlePreview對象已添加爲單元格的子視圖,因此在下載完成後,請在視圖下載內容時調用setNeedsDisplay

+0

下載完成在loadArticlePreview:'[類別downloadArticlesForIndex:[_ idx intValue]]'。我不確定在哪裏調用'setNeedsDisplay',你會不會解釋一下?謝謝 – atnatn 2012-02-06 06:34:56

+0

@atnatn:哦,我的錯誤。沒有看到。忘記setNeedsDisplay。如果您的articleInfos包含下載的數據,則表示您沒有問題。記錄下它是否正確,然後嘗試實施我所建議的內容。只有當您重新下載完成下載時,您的重新加載代碼纔是正確的。意思是,downloadArticlesForIndex:在您重新加載時已經完成下載。 – MadhavanRP 2012-02-06 06:53:51

+0

是的,我想過,但我無法弄清楚如何確保在我重新加載表格的時候下載已經完成。 – atnatn 2012-02-06 07:04:12

0

你不能在後臺線程中重新加載你的tableview。您必須創建這樣
-(void)reloadTable { [self.articlePreviewTableView reloadData]; }

的方法然後-(void) loadArticlePreview: (NSNumber *)_idx方法

希望這能解決問題,烏爾裏調用此方法在主線程上。

+0

不幸的是它沒有。謝謝,雖然:D – atnatn 2012-02-06 07:04:29