2012-02-06 130 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