内存警告后 UITableView 滚动问题
我创建了一个类似于 iOS 地址簿的 UITableView,它允许您在一行上选择图像,并且还有其他自定义单元格,这些单元格具有用于输入数据的文本字段。仅在加载大图像并在控制台中收到内存警告后才遇到此问题:
收到内存警告。 Level=1
我在模态视图中使用 UIImagePickerController 加载图像。
因此,当发生内存警告时,我的父 tableviewcontroller 会执行 viewDidUnload,它是空的:
- (void)viewDidUnload { }
一旦我关闭模态视图(选择图像),tableview 就会重新加载数据,一切看起来都很好......但是,滚动无法正常工作。这种行为非常奇怪:滚动不会停留在某个点之后。我实际上可以将滚动一直拉下来,但是一旦我松开,滚动就会弹回来并隐藏底部的几个单元格(看起来正常)。
对于我的一生,我无法弄清楚发生了什么,为什么这只在内存警告之后发生。
这只发生在我的 iPhone 上(使用 iPhone 4 进行测试)。这进一步让我相信内存警告可能是我的问题的根源。
I created a UITableView similar to the iOS address book that allows you to pick an image on one row and and there are additional custom cells that have text fields for entering data. I run into this problem only after loading a large image and get a memory warning in the console:
Received memory warning. Level=1
I load the image using the UIImagePickerController in a modal view.
So, when the memory warning occurs, my parent tableviewcontroller does a viewDidUnload, which is empty:
- (void)viewDidUnload { }
Once I close the modal view (choosing the image), the tableview reloads data and everything appears fine...However, scrolling does not work properly. The behavior is very odd: The scrolling will not stay past a certain point. I can literally pull the scrolling down all the way, but as soon as I release, the scrolling bounces back up and hides the bottom fiew cells (which appear normal).
For the life of me I cannot figure out what is going on, why this only occurs after a memory warning.
This is only happening on my iPhone (testing with iPhone 4). This further leads me to believe the memory warning is probably the root of my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有一个解决方法。在显示模态视图之前,您应该关闭键盘并等待其关闭。
I have a work around. Before you display the modal view you should dismiss the keyboard and wait until its dismissed.
您在 [loadView] 或 [viewDidLoad] 中设置的所有内容都应该在 [viewDidUnload] 中清理。反之亦然:您在 [viewDidUnload] 中销毁的所有内容都应该在 [loadView] 或 [viewDidLoad] 中重新创建并重新初始化。
您应该将其视为辅助初始化/释放序列。
Everything you set up in [loadView] or [viewDidLoad] should be cleaned up in [viewDidUnload]. And the other way around: everything you destroy in [viewDidUnload] should be re-created and re-initialized in [loadView] or [viewDidLoad].
You should treat it as a secondary init/dealloc sequence.
在这种情况下,将
-(void)viewDidUnload;
设置为空肯定是错误的,因为至少应该调用[super viewDidUnload];
否则你将有两个表在视图(甚至更多)中,因为控制器将继续在-(void)loadView;
内创建它们Setting the
-(void)viewDidUnload;
to empty is definitely wrong in that context as there should be at least call to[super viewDidUnload];
otherwise you'll have two tables in the view(or even more) because the controller will keep creating them inside of-(void)loadView;