Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios <;数组的UITableView滚动索引;FBGraphUser>;物体_Ios_Facebook_Facebook Graph Api_Uitableview - Fatal编程技术网

Ios <;数组的UITableView滚动索引;FBGraphUser>;物体

Ios <;数组的UITableView滚动索引;FBGraphUser>;物体,ios,facebook,facebook-graph-api,uitableview,Ios,Facebook,Facebook Graph Api,Uitableview,真的被这个难倒了需要一些帮助!我正在创建UITableViewController的子类以显示FB好友列表(FBFriendPickServiceController对我有几个限制)。我能够检索id数组并按字母顺序排序 然而,仍然无法从这里找到一种方法来创建一个单独的字典,将FB用户按字母顺序划分为多个部分进行索引 -(void)captureFacebookFriendUsers { //Issue a Facebook Graph API request NSLog(@"%@

真的被这个难倒了需要一些帮助!我正在创建UITableViewController的子类以显示FB好友列表(FBFriendPickServiceController对我有几个限制)。我能够检索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"]];
        }
}
-(无效)captureFacebookFriendUsers
{
//发出Facebook图形API请求
NSLog(@“%@”,NSStringFromSelector(_cmd));
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection*连接,id结果,NSError*错误){
如果(!错误){
NSLog(@“请求好友时无错误”);
friendsObjects=[result objectForKey:@“data”];//对象是id
friendsNames=[NSMutableArray阵列容量:friendsObjects.count];
NSMutableArray*FriendId=[NSMutableArray阵列容量:friendsObjects.count];
//创建好友的Facebook ID列表
NSSortDescriptor*firstNameDescriptor=[[NSSortDescriptor alloc]initWithKey:@“first_name”升序:是];
friendsObjects=[friendsObjects SortedArray使用描述符:@[firstNameDescriptor];
for(friendsObjects中的NSDictionary*FriendsObject){
[friendIds-addObject:[friendObject-objectForKey:@“id”];
[FriendsNamesAddObject:[friendObject objectForKey:@“名字”];
}
}

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

您需要为此使用UILocalizedIndexedCollation,并调用[self.collation sectionForObject:friend collationStringSelector:@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
//查看控制器代码
-(无效)viewDidLoad
{
[超级视图下载];
[自身条件切除];
}
-(无效)配置节
{
//UILocalizedIndexedCollation
self.collation=[UILocalizedIndexedCollation currentCollation];
NSInteger索引,sectionTitlesCount=[[self.collation sectionTitles]计数];
//有数据的新章节
NSMutableArray*newSectionsArray=[[NSMutableArray alloc]initWithCapacity:sectionTitlesCount];
//为每个节分配数据数组
对于(索引=0;索引<节标题计数;索引++){
NSMUTABLEARRY*array=[[NSMUTABLEARRY alloc]init];
[newSectionsArray添加对象:数组];
}
//把朋友放在适当的部分
用于(个人*自我中的朋友。朋友){
//询问排序规则朋友姓名属于哪个节号
NSInteger sectionNumber=[self.collation sectionForObject:friend collationStringSelector:@selector(name)];
//获取该节的数组。
NSMutableArray*sectionFriends=[newSectionsArray对象索引:sectionNumber];
//将好友添加到分区。
[sectionFriends添加对象:friend];
}
//现在所有数据都已就绪,需要对每个节数组进行排序。
对于(索引=0;索引<节标题计数;索引++){
NSMutableArray*friendsArrayForSection=[NewsSectionsArray对象索引:索引];
NSArray*sortedFriendsArrayForSection=[self.collation sortedArrayFromArray:friendsArrayForSection collationString选择器:@selector(name)];
//用已排序的数组替换现有数组。
[newSectionsArray replaceObjectAtIndex:index with object:sortedFriendsArrayForSection];
}
self.sectionsArray=newSectionsArray;
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回[self.sectionsArray count];
}
-(NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分
{
返回[[self.collation sectionTitles]objectAtIndex:section];
}
-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)表格视图
{
返回[self.collation sectionIndexTitles];
}
-(NSInteger)tableView:(UITableView*)分区索引的tableView部分exttitle:(NSString*)标题索引:(NSInteger)索引
{
返回[self.collation sectionforsectionIndexitLeatIndex:index];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[[self.sectionsArray objectAtIndex:section]计数];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
....
Person*Person=[[self.sectionsArray objectAtIndex:indexath.section]objectAtIndex:indexath.row];
....
cell.textlab.text=person.name;
....
返回单元;
}
//将好友数据存储在类Person中,以便将对象传递给
//-(NSInteger)节对象:(id)对象排序规则字符串选择器:(SEL)选择器
//示例Person.h
@接口人:NSObject
@属性(非原子,副本)NSString*id;
@属性(非原子,副本)NSString*名称;
@属性(非原子,副本)NSString*pictureUrl;
-(id)initWithId:(NSString*)id name:(NSString*)name picture:(NSString*)picUrl;
@结束

Andris的代码非常优秀,但您不需要创建单独的Person类来实现此功能。只需使用NSDictionary代替Person类,如下所示:

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

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

     }];
}
} 最后,使用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];
}
//将朋友放入适当的部分
用于(n词汇)
// 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;
}