2012-02-06 5 views
1

サイドバイサイドの2つのUITableViewがあるアプリケーションを作成しようとしています。左側は記事のカテゴリを示し、右側は記事のプレビュー(フリップボードの検索ビューのようなもの)を表示します。サイドバイサイドのデータをリロードするUITableView

左側のテーブルビューのdidSelectRowAtIndexPathでは、記事をダウンロードし、右側のUITableViewにプレビューを表示することになっています。しかし、私はこの仕事をすることはできません。

私は、ダウンロードが完了する前にテーブルビューでデータをリロードすることを前提としています。助言がありますか?

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]]; 
    } 
} 

問題は右のテーブルビューが更新されないということです。

ここに私の現在のコードです。私は、これらの方法はおそらく問題がどこにあるのだろうと思います。

+0

どうやって今やっているのですか?どんな問題を抱えていますか?可能であれば特定し、該当する場合はコードを表示してください。 – sosborn

答えて

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

+0

@atnatn:ああ、私の間違い。それを見ていない。 setNeedsDisplayについては忘れてください。あなたのarticleInfosにダウンロードしたデータが含まれていれば、あなたは大丈夫です。それを記録してその権利があるかどうかを確認し、提案した内容を実装してみてください。リロードするコードは、リロードした時点でダウンロードが完了した場合にのみ有効です。つまり、downloadArticlesForIndex:はリロードするまでにダウンロードを完了しました。 – MadhavanRP

+0

はい、私はそれについて考えましたが、テーブルをリロードした時点でダウンロードが完了していることを確認する方法がわかりません。 – atnatn

0

バックグラウンドスレッドでテーブルビューをリロードすることはできません。あなたは、これはウルの問題を解決-(void) loadArticlePreview: (NSNumber *)_idx方法

希望の内側にメインスレッドでこのメソッドを呼び出して、この
-(void)reloadTable { [self.articlePreviewTableView reloadData]; }

のようなメソッドを作成する必要があります。

+0

残念ながらそれはありません。しかし、D – atnatn

関連する問題