我有一個簡單的應用程序,其中包含應用內設置Table View Controller
。當用戶選擇「應用主題」單元格時,它將segues
(推送)嵌入到UIViewController
中的UICollectionView
。UICollectionView首次加載明顯延遲
這是12x3x4時尚單元格,其中每個單元格都有圖像,標籤和複選標記圖像(如果已選擇該主題)。
細胞在這裏是動態加載的,但是當我第一次繼續這個UICollectionView
時,我注意到了一些重大的延遲。隨後的每一次,它都能正常工作,但在iPhone 5s上,有一些極端明顯的滯後,我對iOS開發相當陌生,我有一種感覺,這與第一次創建視圖的方式有關,但我我不確定。
在我viewDidLoad
,我有:
- (void)viewDidLoad
{
self.title = @"Themes";
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
[super viewDidLoad];
self.cView.dataSource = self;
self.cView.delegate = self;
self.themeLabels = [[NSArray alloc] initWithObjects:@"Original", @"Peacock", @"Mystical", @"Zebra", @"Simplicity", @"Rainbow", @"Prosperity", @"Leopard", @"Hypnotic", @"Dunes", @"Twirl", @"Oceanic", nil];
self.themeImages = @[[UIImage imageNamed:@"Newiphonebackground.png"], [UIImage imageNamed:@"peacock.png"], [UIImage imageNamed:@"Purplepink.png"], [UIImage imageNamed:@"PinkZebra.png"], [UIImage imageNamed:@"Greenish.png"], [UIImage imageNamed:@"MarblePrint.png"], [UIImage imageNamed:@"Prosperity.png"], [UIImage imageNamed:@"leopard.png"], [UIImage imageNamed:@"CircleEffect.png"], [UIImage imageNamed:@"Orange3.png"], [UIImage imageNamed:@"ReddishBlack.png"], [UIImage imageNamed:@"bluey.png"]];
[self changeAppThemes];
}
的changeAppThemes方法基本上是通過和NSUserDefaults的循環眼看要應用的主題。
- (void)changeAppThemes
{
self.selectedTheme = [[NSUserDefaults standardUserDefaults] objectForKey:@"Theme"];
if ([self.selectedTheme isEqualToString:@"Mystical"])
{
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Purplepink.png"]];
UIImage *navBackgroundImage = [[UIImage imageNamed: @"Purplepinknav"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[self.navigationController.navigationBar setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
UIImage *tabBackground = [[UIImage imageNamed:@"SolidPurple.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[self.tabBarController.tabBar setBackgroundImage:tabBackground];
}
etc
的viewWillAppear
是:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self changeAppThemes];
self.selectedThemeString = [[NSUserDefaults standardUserDefaults] objectForKey:@"Selection"];
if ([self.selectedThemeString isEqualToString:@"Original"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
}
else if ([self.selectedThemeString isEqualToString:@"Oceanic"])
{
self.checkedIndexPath = [NSIndexPath indexPathForRow:11 inSection:0];
}
[self.cView reloadData];
}
的viewWillAppear
今天之前只有在調用changeAppThemes
方法,無需其他代碼,但segueing到UICollectionView
首次當它還是奇慢無比。
的cellForItemAtIndexPath
是:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ThemeCell *themeCell = (ThemeCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Theme Cell" forIndexPath:indexPath];
NSString *cellData = [self.themeLabels objectAtIndex:indexPath.row];
themeCell.cellLabel.text = cellData;
themeCell.cellImages.image = self.themeImages[indexPath.row];
UIImageView *dot = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 1, 2)];
dot.image=[UIImage imageNamed:@"check-white-hi.png"];
if([self.checkedIndexPath isEqual:indexPath])
{
NSLog(@"Loaded");
themeCell.backgroundView = dot;
[themeCell addSubview:dot];
}
else
{
NSLog(@"Not Loaded");
themeCell.backgroundView = nil;
}
我應該改變,我發出呼叫到changeAppThemes
?我已經從viewWillAppear
中刪除了該呼叫,並將其留在了viewDidLoad
之間,反之亦然,在這兩種情況下,第一次打開UICollectionView
時的響應性非常差。
任何想法將非常感激。
檢查你的圖片尺寸 – NeverBe
這實際上是一個非常有趣的觀點。我實際上使用相同的圖像作爲應用程序的背景 - 所以iPhone 5的大小爲640x1136,用於此收藏單元的圖像。現在你已經提到了圖像大小,這似乎有點過分。我應該大量減少圖像的大小並進行測試嗎? – amitsbajaj
使用圖像的大小,你設計它乘以2爲視網膜 – NeverBe