gpt4 book ai didi

ios - 对象数组的 UITableView 滚动索引

转载 作者:行者123 更新时间:2023-11-29 03:37:33 24 4
gpt4 key购买 nike

真的很困惑,需要一些帮助!我正在创建 UITableViewController 的子类来显示 FB 好友列表(FBFriendPickerViewController 对我来说有一些限制)。我能够检索 id 数组并按字母顺序对它们进行排序。

但是,仍然无法从这里找到一种方法来创建单独的字典,将 FB 用户分为按字母顺序排列的部分以进行索引。

-(void)captureFacebookFriendUsers
{
//Issue a Facebook Graph API request
NSLog(@"%@", NSStringFromSelector(_cmd));
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error){
if (!error) {
NSLog(@"No error requesting friends");
friendsObjects = [result objectForKey:@"data"]; //Objects are id<FBGraphUser>
friendsNames = [NSMutableArray arrayWithCapacity:friendsObjects.count];
NSMutableArray *friendIds = [NSMutableArray arrayWithCapacity:friendsObjects.count];
//Create a list of friends' Facebook IDs
NSSortDescriptor *firstNameDescriptor = [[NSSortDescriptor alloc]initWithKey:@"first_name" ascending:YES];
friendsObjects = [friendsObjects sortedArrayUsingDescriptors:@[firstNameDescriptor]];

for (NSDictionary *friendObject in friendsObjects) {
[friendIds addObject:[friendObject objectForKey:@"id"]];
[friendsNames addObject:[friendObject objectForKey:@"first_name"]];
}
}

感谢您花时间阅读本文!

最佳答案

Andris 的代码非常好,但您不需要为此创建一个单独的 Person 类。只需使用 NSDictionary 代替 Person 类,如下所示:

首先,创建您的字典 - 我在 View Controller 中执行此操作,我将从中调用 TableView 作为我的按钮操作的一部分。您还需要在两个 .h 文件(用于初始 View Controller 和 TableView Controller )中声明一个 NSArray *friends 和一个 NSNumber *friendsCount 属性,并合成为 _friends _friendCount。

- (IBAction)btnAddFriendsTapped:(UIBarButtonItem *)sender {

if (FBSession.activeSession.isOpen){

__block NSArray *friendsArray = [[NSArray alloc]init];
__block NSNumber *friendsArrayCount = [[NSNumber alloc]init];

FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
friendsArray = [result objectForKey:@"data"];
friendsArrayCount = [NSNumber numberWithInt:friendsArray.count];

NSLog(@"Found: %i friends", [friendsArrayCount intValue]);

for (NSDictionary<FBGraphUser>* friend in friendsArray) {
NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);
_friends = [NSArray arrayWithArray:friendsArray];
_friendCount = [NSNumber numberWithInt:[friendsArrayCount intValue]];
}
[self performSegueWithIdentifier:@"friendListSegue" sender:self];

}];
}

然后在 prepareForSegue 方法中将您的字典传递给 TableView Controller ,不要忘记先导入您的 TableView Controller 头文件。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"friendListSegue"]){
BJFriendListTVC* listOfFriends = segue.destinationViewController;
listOfFriends.friends = _friends;
listOfFriends.friendCount = _friendCount;
}

}最后以Andris的表代码替换Person类

// Put friends into the appropriate sections
for (NSDictionary<FBGraphUser> *friend in self.friends) {

// Ask the collation which section number the friend name belongs in
NSInteger sectionNumber = [self.collation sectionForObject:friend collationStringSelector:@selector(name)];

// Get the array for that section.
NSMutableArray *sectionFriends = [newSectionsArray objectAtIndex:sectionNumber];

// Add the friend to the section.
[sectionFriends addObject:friend];
}

然后当您配置单元时:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary<FBGraphUser> *person = [[self.sectionsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = person.name;
return cell;
}

关于ios - <FBGraphUser> 对象数组的 UITableView 滚动索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18953928/

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