0

真的被这件事难住了,需要一些帮助!我正在创建 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"]];
        }
}

感谢您花时间阅读本文!

4

2 回答 2

1

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

首先,创建您的字典 - 我在视图控制器中执行此操作,我将在其中调用表格视图作为按钮操作的一部分。你还需要在你的两个 .h 文件中声明一个 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 方法中将您的字典传递给 Table View Controller,不要忘记首先导入您的 Table View 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;
}
于 2014-03-04T14:17:51.773 回答
0

您需要为此使用 UILocalizedIndexedCollat​​ion 并调用 [self.collat​​ion sectionForObject:friend collat​​ionStringSelector:@selector(name)] 以获取与设备区域设置的朋友名称相对应的部分的索引。为此,您需要将朋友数据存储在具有属性“名称”的类中(可能有一种方法可以继续将 NSDictionary 用于我不知道的朋友数据)。

这是一些代码:

// View Controller code

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self condigureSections];
}

- (void)configureSections
{
    // UILocalizedIndexedCollation
    self.collation = [UILocalizedIndexedCollation currentCollation];

    NSInteger index, sectionTitlesCount = [[self.collation sectionTitles] count];
    // new sections with data
    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];

    // allocate data array for each of the sections
    for (index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [newSectionsArray addObject:array];
    }


    // Put friends into the appropriate sections
    for (Person *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];
    }


    // Now that all the data's in place, each section array needs to be sorted.
    for (index = 0; index < sectionTitlesCount; index++) {

      NSMutableArray *friendsArrayForSection = [newSectionsArray objectAtIndex:index];

      NSArray *sortedFriendsArrayForSection = [self.collation sortedArrayFromArray:friendsArrayForSection collationStringSelector:@selector(name)];

      // Replace the existing array with the sorted array.
      [newSectionsArray replaceObjectAtIndex:index withObject:sortedFriendsArrayForSection];
    }

    self.sectionsArray = newSectionsArray;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.sectionsArray count];
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[self.collation sectionTitles] objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [self.collation sectionIndexTitles];
}


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return [self.collation sectionForSectionIndexTitleAtIndex:index];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.sectionsArray objectAtIndex:section] count];
}



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



// Store friend data in a class Person so that we could pass the object to 
// - (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector

// Example Person.h
@interface Person : NSObject

@property (nonatomic, copy) NSString *id;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *pictureUrl;

- (id)initWithId:(NSString *)id name:(NSString *)name picture:(NSString *)picUrl;

@end
于 2013-09-23T08:48:45.810 回答