gpt4 book ai didi

ios - 如何将 5 个不同的头像应用到 5 个不同的 UIButton?

转载 作者:行者123 更新时间:2023-11-29 10:31:45 26 4
gpt4 key购买 nike

我正在使用 for 循环创建 5 个 UIButton,并想在 UIButton 上放置 5 个不同的头像。但是,只有一个头像填满了所有 5 个按钮。

我构建的代码如下所示,

    // create a subview for avatar buttons
UIView *avatarView = [[UIView alloc] init];
avatarView.frame = CGRectMake(20, 125, 280, 100); // don't mess with these values.
// avatarView.layer.borderColor = [UIColor redColor].CGColor;
// avatarView.layer.borderWidth = 3.0f;
[self.view addSubview:avatarView];

UIScrollView *avatarScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
avatarScroll.contentSize = CGSizeMake(500, 500);
avatarScroll.scrollEnabled = YES;
[avatarView addSubview:avatarScroll];


// fetch Data from Core Data
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:_managedObjectContext];
[fetchRequest setEntity:entity];

// fetch records and handle error
NSError *error;
NSArray *results = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];

// sort results array by lastLogin
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"lastLogin" ascending:NO];
NSArray *sortedArray = [NSArray arrayWithObject:sort];
NSArray *sortedArray2 = [results sortedArrayUsingDescriptors:sortedArray];
// how to remove values from NSArray
NSArray *lastLoginArray = [sortedArray2 valueForKey:@"lastLogin"];
// NSLog(@"lastLoginArray = %@",lastLoginArray);
// make an array that only hold 5 values
// NSArray *last5LoginArray;
// for (int i=5; i<[lastLoginArray count]; i++) {
// [last5LoginArray addObject:[lastLoginArray objectAtIndex:i]];
// }
// NSLog(@"last5LoginArray = %@",last5LoginArray);
// NSArray *last5LoginArray = [NSArray arrayWithObjects:lastLoginArray count:4];
// NSArray *last5LoginArray = [NSArray arrayByAddingObjectsFromArray:lastLoginArray count:4];
NSMutableArray *last5LoginArray = [[NSMutableArray alloc] initWithArray:[lastLoginArray subarrayWithRange:NSMakeRange(0, 5)] ];
NSLog(@"last5LoginArray = %@",last5LoginArray);

// NSLog(@"sortedArray2 = %@",sortedArray2);

CGFloat staticX = 0;
CGFloat staticWidth = 80;
CGFloat staticHeight = 80;
CGFloat staticPadding = 5;

// need to put the avatars stored in sortedArray2 in the scrollView
for ( int i = 0; i < 5; i++) {
// do additional loading for avatars
UIButton *avatarButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// the last two values control the size of the button
// avatarButton.frame = CGRectMake(0, 0, 80, 80);
[avatarButton setFrame:CGRectMake((staticX + (i * (staticHeight + staticPadding))),5,staticWidth,staticHeight)];
// make corners round
avatarButton.layer.cornerRadius = 40; // value varies -- // 35 yields a pretty good circle.
avatarButton.clipsToBounds = YES;
// create a stock image
UIImage *btnImage = [UIImage imageNamed:@"HomeBrewPoster1.jpg"];
Account *anAccount;
for ( anAccount in results) {
if([last5LoginArray containsObject:anAccount.lastLogin]) {
NSLog(@"anAccount.lastLogin = %@",anAccount.lastLogin);
UIImage *avatarImg = [UIImage imageWithData:anAccount.avatar ];
// apply avImg to btn
[avatarButton setBackgroundImage:avatarImg forState:UIControlStateNormal];
}
}
if (btnImage == nil) {
NSLog(@"can't find HomeBrewPoster1.jpg");
// apply stock image to button(s)
[avatarButton setBackgroundImage:btnImage forState:UIControlStateNormal];
} else {

}
// this should add 5x buttons
[avatarScroll addSubview:avatarButton];
}

最佳答案

你最里面的 for-in 循环总是将 last 匹配的头像设置为按钮,因为 i 的当前值完全没有区别最内层循环的作用。

您应该将嵌套循环移到第一个循环之外以预先准备五个头像,然后使用 i 索引到头像数组中,如下所示:

NSMutableArray *avatars = [NSMutableArray arrayWithCapacity:5];
for ( anAccount in results) {
if([last5LoginArray containsObject:anAccount.lastLogin]) {
NSLog(@"anAccount.lastLogin = %@",anAccount.lastLogin);
UIImage *avatarImg = [UIImage imageWithData:anAccount.avatar ];
[avatars addObject:avatarImg];
}
}
NSAssert(
avatars.count == last5LoginArray.count
, @"The loop is expected to find as many avatars as there are items in last5LoginArray"
);
for ( int i = 0; i < 5; i++) {
...
// Check that we have enough logins
if (i < last5LoginArray.count) {
[avatarButton setBackgroundImage:avatars[i] forState:UIControlStateNormal];
}
}

关于ios - 如何将 5 个不同的头像应用到 5 个不同的 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29247936/

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