奇怪的滚动问题( UIScrollView )
Posted
技术标签:
【中文标题】奇怪的滚动问题( UIScrollView )【英文标题】:Odd Scrolling Issue ( UIScrollView ) 【发布时间】:2011-02-21 15:07:00 【问题描述】:我找了好久都没有用,希望有人能帮忙!
我在加载自定义单元格/子视图的 UIView 的左半部分有一个 UIScrollView。 UIView 是 UINavigation 堆栈的一部分,它也被加载到 TabBar 上的选项卡中。
发生的情况是,如果我启动应用程序并立即开始滚动,它会非常流畅。但是,如果我启动应用程序并等待 5-10 秒,则 UIScrollView 非常滞后且不稳定(并且一直如此)。我认为这可能是内存泄漏或其他原因,但我似乎找不到任何东西。
包括我将自定义单元格/子视图加载到 UIScrollView 中的视图代码。单元格子视图中没有任何自定义代码。哦,只有大约 8-10 个项目:每个项目都有一个小 (150x150) 图像和 3 个文本字段 - 都是不透明的。
#import "ProductListViewController.h"
#import "ProductListLeftItemViewController.h"
@implementation ProductListViewController
@synthesize listScroll;
- (void)viewDidLoad
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"Products" ofType:@"plist"];
NSArray *products = [[NSArray alloc] initWithContentsOfFile:path];
//
int numProducts;
numProducts = [products count];
[listScroll setContentSize:CGSizeMake(500, (numProducts * 111))];
for (int i = 0; i < numProducts; i++)
ProductListLeftItemViewController *cellItem = [[ProductListLeftItemViewController alloc] initWithNibName:@"ProductListLeftItem" bundle:nil];
cellItem.view.frame = CGRectMake(0, (i*111), 500, 111);
[self.listScroll addSubview:cellItem.view];
cellItem = nil;
[cellItem release];
[products release];
[super viewDidLoad];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
NSLog(@"Memory warning, ProductListViewController");
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
- (void)viewDidUnload
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (void)dealloc
[listScroll release];
[super dealloc];
@end
根据要求,ProductListLeftItemViewController 代码:
#import "ProductListLeftItemViewController.h"
@implementation ProductListLeftItemViewController
@synthesize titleTxt, descriptionTxt, modelTxt, productThumb;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
// Custom initialization.
return self;
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
[super viewDidLoad];
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES;
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
- (void)viewDidUnload
titleTxt = nil;
descriptionTxt = nil;
modelTxt = nil;
productThumb = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (void)dealloc
[titleTxt release];
[descriptionTxt release];
[modelTxt release];
[productThumb release];
[super dealloc];
@end
【问题讨论】:
你是从 URL 加载图片吗? 你有多少产品?你能告诉我们你的 ProductListLeftItemViewController 吗?顺便说一句,将 cellItem 设置为 nil 后在 cellItem 上调用 release 是无稽之谈。你应该交换这两行。 Harinder:我没有从 URL 加载图像,事实上,我清除了整个子视图,所以它只是一个不透明的白色背景——结果相同。主要是 Izzy:只有 8 种产品,但我什至只用 1 种产品尝试过,而且信息很少 - 结果相同。谢谢,我换了台词。 【参考方案1】:对于将来遇到此问题的任何人,一旦我开始在实际设备上进行测试,一切都非常顺利。也许这只是模拟器的古怪之处?我会认为这已解决,尽管我仍将进一步调查。
【讨论】:
以上是关于奇怪的滚动问题( UIScrollView )的主要内容,如果未能解决你的问题,请参考以下文章
尽管内容大小正确,但 UIScrollview 不会滚动; textview 子视图框架也表现得很奇怪
CATransform3D 奇怪的 UIScrollView contentOffset