使用UItableview在iOS应用开发中实现好友列表功能

2020-01-14 17:58:21于海丽

        NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"friends.plist" ofType:nil];
        NSArray *arrayM=[NSArray arrayWithContentsOfFile:fullpath];
        
        NSMutableArray *models=[NSMutableArray arrayWithCapacity:arrayM.count];
        for (NSDictionary *dict in arrayM) {
            YYQQGroupModel *group=[YYQQGroupModel qqGroupModelWithDict:dict];
            [models addObject:group];
        }
        _groupFriends=[models copy];
    }
    return _groupFriends;
}

 

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.tableView.sectionHeaderHeight = 100;
}

#pragma mark-  设置数据源
//返回多少组
//为什么这里不会智能提示?因为这些方法是uitableview协议里的,默认并没有遵守协议,让主控制器类继承uitableviewcontroller后,就已经遵守了
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groupFriends.count;
}
//每组返回多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //取出对应的组模型
    YYQQGroupModel *group=self.groupFriends[section];
    //返回对应组中的好友数
    return group.friends.count;
}
//每组每行的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.创建cell
    YYfriendCell *cell=[YYfriendCell cellWithTableview:tableView];

    //2.设置cell
    YYQQGroupModel *group=self.groupFriends[indexPath.section];
    YYFriendsModel *friends=group.friends[indexPath.row];
    cell.friends=friends;
    //3.返回一个cell
    return cell;
}


#pragma mark - 代理方法
// 当一个分组标题进入视野的时候就会调用该方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section