gpt4 book ai didi

ios - UICollectionView 第一次加载时明显滞后

转载 作者:行者123 更新时间:2023-11-28 19:03:20 24 4
gpt4 key购买 nike

我有一个简单的应用程序,其中包含应用程序内设置 Table View Controller。当用户选择“App Themes”单元格时,它会segues(推送)到嵌入在 UIViewController 中的 UICollectionView

这是 12 个 3x4 格式的单元格,其中每个单元格都有一个图像、一个标签和一个复选标记图像(如果已选择该主题)。

单元格在此处的类中动态加载,但我注意到在第一次转到此 UICollectionView 时出现一些重大延迟。随后的每一次,它都运行良好,但在 iPhone 5s 上,有一些明显的延迟,我对 iOS 开发还很陌生,我觉得这与第一次创建 View 的方式有关,等等,但我不确定。

在我的 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 方法,没有其他代码,但是在转到 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 时响应速度都非常糟糕 第一次。

如有任何想法,我们将不胜感激。

最佳答案

异步加载您的图片。考虑下面的变体。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

dispatch_async(queue, ^{
//Do THIS
UIImage *localImage [self createImageObjectsforIndex:indexPath.row];
dispatch_sync(dispatch_get_main_queue(), ^{
//Do this when done
cell.image = localImage;
});

});

关于ios - UICollectionView 第一次加载时明显滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22788239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com