1

I am using a standard navigator interface on the iPhone. When I move from the highest level tableview to the next level, I have to load about 2000 records from core data, which can take 5-10 seconds, so I want to use an activity indicator.

I have done the following code which works if I remove the code in the DidSelect method, but otherwise the indicator never appears, even though the view still waits there while the other view loads. Here are the methods:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{   
[tableView deselectRowAtIndexPath:indexPath animated:YES];

//-- start activity indicator (will be stopped when view disappears)
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:m_activityIndicator];
[m_activityIndicator startAnimating];


return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {



//--show the listViewController
CardListViewController *listViewController = [[CardListViewController alloc] initWithNibName:@"CardListViewController" bundle:nil];
NSString* selectedCardTypeName = [rootSelectionList objectAtIndex:indexPath.row];
enum CardTypes selectedCardType = cardTypeIDFromCardTypeName(selectedCardTypeName);
listViewController.selectedCardType =selectedCardType;
[self.navigationController pushViewController:listViewController animated:YES];
[listViewController release];


}

How can I force the activity indicator to show up before it starts processing the the next listviewcontroller?

4

1 回答 1

3

首先,为什么需要同时加载 2k 行?为什么不偷懒呢?

首先,在加载表格视图时,您将使用数据源仅计算要显示的行数,查询核心数据以仅获取数字,因此您的表格视图将准备好显示所有必要的单元格。

其次,从核心数据只加载你需要的内容,这意味着,只加载屏幕上的内容。想象一下,如果用户非常快速地拖动和滚动表格视图,许多单元格将被加载而无需它。正确的做法是仅加载可见单元格,这意味着在滚动过程停止时可见的单元格。

看看 Apple 的这个示例,了解如何延迟加载表格视图的资源:http: //developer.apple.com/iphone/library/samplecode/LazyTableImages/Listings/Classes_RootViewController_m.html#//apple_ref/doc/uid/ DTS40009394-Classes_RootViewController_m-DontLinkElementID_12

它适用于图像,但您可以对获取对象执行相同的想法。

懒惰地这样做你不需要活动指示器。

顺便说一句,回到活动指示器的问题,您可能需要让您的单元格更新:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{   
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    //-- start activity indicator (will be stopped when view disappears)
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell.contentView addSubview:m_activityIndicator];
    [m_activityIndicator startAnimating];
    [cell setNeedsDisplay];

    return indexPath;
}

干杯

于 2010-08-27T02:55:50.943 回答