开发者

How to switch between UIViews without lost the data?

开发者 https://www.devze.com 2023-04-11 05:49 出处:网络
I build an iphone app and there is you know multiple views to switch between them, but I face a problem when I switch between them and its like this:

I build an iphone app and there is you know multiple views to switch between them, but I face a problem when I switch between them and its like this:

The second view that I switch to has a UITableView and every time shows up its loaded with 4 rows but when I switch to the third view and try to get back to the second one which is the table all the rows disappear.

My iPhone template is viewbased app. The code used to switch between the vies like this :

 if (self.webController==nil) {
    SearchWebViewController *d=[[SearchWebViewController alloc] initWithNibName:@"SearchWebViewController" bundle:nil];
    self.webController=d;
    [d release];
}
[UIView beginAnimations:@"flipping view" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
                       forView:self.view开发者_StackOverflow中文版 cache:YES];
[self.view addSubview:webController.view];
[UIView commitAnimations];

How to switch between the views and keep the data in it?

Any idea please and keep it simple as much as possible :)


You need to understand how UITableView works. It does not "remember" what rows are displayed, but calls cellForRowAtIndexPath for every row it needs to display, either when you scroll or when you hide and then redisplay the table.

So you must keep a "backup copy" of all the rows that you're displaying or might -- both those on the screen and those scrolled off the top or bottom.

Usually this "backup copy" is an NSMutableArray of either NSMutableDictionaries or custom objects, each representing one row. When cellForRowAtIndexPath is invoked you access the values in the "backup copy" and create a cell to present the data. If something is changed in the cell, you copy that change to the "backup copy".

0

精彩评论

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

关注公众号