如何在UIView之间切换而不丢失数据?

发布于 2024-12-08 12:48:12 字数 809 浏览 0 评论 0原文

我构建了一个 iphone 应用程序,并且有多个视图可以在它们之间切换,但是当我在它们之间切换时我遇到一个问题,如下所示:

我切换到的第二个视图有一个 UITableView 并且每次都显示它加载4 行,但是当我切换到第三个视图并尝试返回到第二个视图(即表)时,所有行都消失了。

我的 iPhone 模板是基于视图的应用程序。用于在视图之间切换的代码如下:

 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 cache:YES];
[self.view addSubview:webController.view];
[UIView commitAnimations];

如何在视图之间切换并保留其中的数据?

任何想法请并尽可能保持简单:)

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 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天的雪花 2024-12-15 12:48:12

您需要了解 UITableView 的工作原理。它不会“记住”显示哪些行,而是在滚动或隐藏然后重新显示表格时,为需要显示的每一行调用 cellForRowAtIndexPath。

因此,您必须保留正在显示或可能显示的所有行的“备份副本”——包括屏幕上的行和从顶部或底部滚动的行。

通常这个“备份副本”是 NSMutableDictionaries 或自定义对象的 NSMutableArray,每个对象代表一行。当调用 cellForRowAtIndexPath 时,您可以访问“备份副本”中的值并创建一个单元格来显示数据。如果单元格中发生某些更改,您可以将该更改复制到“备份副本”。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文