Iphone 在并排UITableView上重新加载数据
我正在尝试创建一个应用程序,其中有两个UITableView并排放置。左侧列出文章类别,右侧显示文章预览(有点像flipboard的搜索视图) 在左侧tableview的Iphone 在并排UITableView上重新加载数据,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我正在尝试创建一个应用程序,其中有两个UITableView并排放置。左侧列出文章类别,右侧显示文章预览(有点像flipboard的搜索视图) 在左侧tableview的didSelectRowAtIndexPath上,我应该下载文章并在右侧UITableView上显示预览。然而,我似乎无法做到这一点 我的假设是,在下载完成之前,我会在tableview上重新加载数据。有什么建议吗 已编辑: 以下是我当前的代码: - (UITableViewCell *)tableView:(UITableVi
didSelectRowAtIndexPath
上,我应该下载文章并在右侧UITableView上显示预览。然而,我似乎无法做到这一点
我的假设是,在下载完成之前,我会在tableview上重新加载数据。有什么建议吗
已编辑:
以下是我当前的代码:
- (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不刷新。我认为这些方法可能就是问题所在。您无法在后台线程中重新加载tableview。您必须创建这样的方法
-(void)重载表
{
[self.articlePreviewTableView重载数据];
}
然后在-(void)loadArticlePreview:(NSNumber*)idx
方法内的主线程上调用此方法
希望这能解决您的问题。根据您的代码,如果您将
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
类。我发现这是添加子视图在单元格中工作的唯一方式,而且使用自定义类管理单元格要容易得多
我假设您正在以某种方式在
文章预览
中进行下载。下载完成后,无需重新加载tableView。由于已将ArticlePreview
对象添加为单元格的子视图,因此在其中完成下载后,在下载视图内容时调用setNeedsDisplay
。您现在是如何尝试的,遇到了哪些问题?请尽可能具体并显示代码(如果适用)。下载在loadArticlePreview:[Category downloadArticlesForIndex:[\u idx intValue]]
中完成。我不太确定在哪里调用setNeedsDisplay
,您愿意解释一下吗?thanks@atnatn字体哦,我错了。我没看到。忘记设置需要显示。如果您的文章信息包含下载的数据,您就可以了。记录它,看看它是否正确,并尝试实现我的建议。只有在重新加载时下载完成,重新加载的代码才是正确的。意思是,下载的Articles ForIndex:在您重新加载时已完成下载。是的,我考虑过,但我不知道如何确保在我重新加载表时已完成下载。这很愚蠢。我把所有东西都放在if(cell==nil)中,这是所有问题的根源。谢谢你指出这一点,很抱歉昨天忽略了这一点。修正了它:D@atnatn很高兴能帮上忙。你是怎么下载的?它是发生在Category方法中的一个单独线程中,还是下载阻止了应用程序?