UITableview crash during pinch zooming
iOS5: UITableview crash during pinch zoom开发者_如何学运维ing
Steps: 1. Open table 2. Zoom by pinch gesture Crash on iOS5!!! SIGABRT on iOS 4.x it works fine What may be the reason?Signal SIGABRT after
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return tableView;
}
*** Terminating app due to uncaught exception 'NSGenericException', reason:
'The view returned from viewForZoomingInScrollView: must be a subview of the scroll view.
It can not be the scroll view itself.'
I just installed XCode 4.2, and iOS5 on my test iPad today, and I got the same problem.
I could solve the problem by observing the sample code from Apple named ScrollViewSuite. It contains an app named TapToZoom that shows the proper implementation of the pinch zooming (that works in iOS5). Basically, I just had then to do three things: 1- define a constant at the beginning of the .m file of the view controller (just after the #import):
#define ZOOM_VIEW_TAG 100
2-Then, I added a line at the beginning of viewDidLoad (or loadView if you don't use a nib file and create your view through coding - as for the Apple example) that sets a tag on your tableView (the table embedded in the scrollView):
[tableView setTag:ZOOM_VIEW_TAG];
3- Then, finally, change the value returned in the viewForZoomingInScrollView, from the previous 'return tableView' to:
return [scrollView viewWithTag:ZOOM_VIEW_TAG];
Once these three elements were in place, my zooming not only was working again, but I realize it is now much smoother than before - which means that my previous implementation was already deficient in iOS4.
I believe that if you follow these directions, you problem should be solved. Please tell me if not. (technically, you could set the tag of your tableView in the interface window, then get rid of steps one and two, and just give the tag number you defined in the step 3, but on the long run the code would be less readable - at least in my opinion)
The error message is actually helpful. My app that worked fine on iOS4 also started to give this error on iOS 5. I solved the problem by adding a subview to my scrollview (in your case the tableView) and then returning that subview in the delegate.
dummyView = [[UIView alloc] initWithFrame:tableView.bounds];
[tableView addSubview:dummyView];
...
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return dummyView;
}
Same error for me and the solution were simply return the self.view instead myScrollView
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.view;;
}
I hope this helpful.
精彩评论