用于<fbgraphuser>对象</fbgraphuser>数组的UITableView滚动索引

时间:2013-09-23 07:33:49

标签: ios facebook facebook-graph-api uitableview

真的难过这需要一些帮助!我正在创建一个UITableViewController的子类来显示FB Friends列表(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"]];
        }
}

感谢您花时间阅读此内容!

2 个答案:

答案 0 :(得分:1)

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

首先,创建你的字典 - 我在View Controller中执行此操作,我将从中调用表视图作为我的按钮操作的一部分。您还需要在两个.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,不要忘记首先导入表视图控制器头文件。

-(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;
}

答案 1 :(得分:0)

您需要为此使用UILocalizedIndexedCollat​​ion并调用[self.collat​​ion sectionForObject:friend collat​​ionStringSelector:@selector(name)]以获取与设备的语言环境的朋友名称对应的部分的索引。 为此,您需要将朋友数据存储在具有属性“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