在并行UITableView上重新加载数据

时间:2012-02-06 02:47:13

标签: iphone objective-c uitableview

我正在尝试创建一个并排放置两个UITableView的应用程序。左侧列出了文章类别,右侧列出了文章预览(类似于flipboard的搜索视图)。

在左侧tableview的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没有刷新。我认为这些方法可能就是问题所在。

2 个答案:

答案 0 :(得分:0)

您无法在后台线程中重新加载tableview。你必须创建一个像这样的方法     -(void)reloadTable { [self.articlePreviewTableView reloadData]; }

然后在-(void) loadArticlePreview: (NSNumber *)_idx方法

中的主线程上调用此方法

希望这能解决你的问题。

答案 1 :(得分: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