开发者

iPhone - Update current view before pushing new ViewController on UITableViewCell selected

I have a UITableView with a list of items. On selection of an item I'm navigating to a new view that displays the details of the selected item using:

DetailsVC *detailsView = [[DetailsVC alloc] initWithNibName:@"DetailsVC" bundle:nil];
[self.navigationController pushViewController:detailsView animated:YES];

Now, the details vie开发者_如何学Pythonw is getting the data from the remote location so on slow connection it can take a few seconds. What I want to do is display an activity indicator over the selected row on selection.

The problem is the display of the added indicator gets delayed until the next view is ready to navigate to, which makes the indicator usless.

I've tried to add this indicator in those 2 events with the same effect:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Is there a way to add the indicator (or more general, modify the content of a UITableViewCell) in the moment of selection, before navigation occurs.

As a experiment I've also tried to pop up an alert view in the same two events which resulted in poping up the alert after navigation to the details view.


So it's [[DetailsVC alloc] init...] that takes a few seconds, right? This problem is that any view changes you make don't fully take effect until returning all the way back to the runloop, so even if you setup the indicator before creating your object, it too is waiting for the init to finish to make itself visible. What you need to do is defer the creation of your DetailsVC until after the indicator is setup.

I might be a simpler change to use blocks but I can't recall the details of that off the top of my head (haven't used blocks much since code I've been writing lately has had to stay compatible with 3.x). But to use performSelector is easy too, take those 2 lines you first quoted in your question and put them into its own method, such as:

- (void)pushDetailsView {
  DetailsVC *detailsView = [[DetailsVC alloc] initWithNibName:@"DetailsVC" bundle:nil];
  [self.navigationController pushViewController:detailsView animated:YES];
}

And where you had those lines before, setup your indicator and then do this (a delay of 0 doesn't mean to called it immediately, but rather ASAP after returning all the way out of the current call stack):

[self performSelector:@selector(pushDetailsView) withObject:nil withDelay:0]


you can call cell = [self rowForIndexPath:indexPath] in one of your functions. and then add the indicator to cells subview


You can either

  1. add the indicator when u select row and push new view when data came (simultaneously remove indicator at that time) or
  2. Push new view, call web service to get corresponding cell data (in viewDidLoad or viewWillAppear) and display indicator simultaneously and when new data comes remove indicator
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜