首页 > 学院 > 开发设计 > 正文

qq好友列表(tableview的使用)

2019-11-06 09:39:05
字体:
来源:转载
供稿:网友

//

//  ViewController.m

//  QQ好友列表

//

//  Created by 洪福清 on 2016/12/13.

//  Copyright © 2016年 BJTYL. All rights reserved.

//

#import "ViewController.h"

#import "PersonModel.h"

#import "GroupModel.h"

#import "MyTableViewCell.h"

#import "MyHeadView.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,MyHeadViewDelegate>

@PRoperty (strong,nonatomic) UITableView *tableView;

@property (strong,nonatomic) NSArray *grupArray;

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    

 

    self.view.backgroundColor = [UIColorwhiteColor];

    [self.viewaddSubview:self.tableView];

    

    

}

-(NSArray *)grupArray

{

    if (_grupArray ==nil) {

        NSString *path = [[NSBundlemainBundle] pathForResource:@"test.plist"ofType:nil];

        NSArray *arrDict = [NSArrayarrayWithContentsOfFile:path];

        

        

        NSMutableArray *arrayModels = [NSMutableArrayarray];

        for (NSDictionary *dictin arrDict) {

            GroupModel *models = [GroupModelGroupWithDict:dict];

            [arrayModels addObject:models];

        }

        _grupArray = arrayModels;

    }

    return_grupArray;

  

}

-(UITableView *)tableView

{

    if (_tableView ==nil) {

        _tableView = [[UITableViewalloc] initWithFrame:CGRectMake(0,64, 375, 667-64)style:UITableViewStylePlain];

        _tableView.delegate =self;

        _tableView.dataSource =self;

        _tableView.bounces =NO;

        _tableView.showsHorizontalScrollIndicator =NO;

        _tableView.showsVerticalScrollIndicator =NO;

        _tableView.separatorStyle =UITableViewCellSeparatorStyleNone;

        _tableView.sectionHeaderHeight =44;

        

    }

    return_tableView;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    returnself.grupArray.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    

    // 在这个方法中。要根据当前组的状态)是否是展开)

    

    GroupModel *models =self.grupArray[section];

    if (models.isVisible) {

         return models.friedns.count;

    }

    else

    {

        return0;

    }

}

static NSString *cellId =@"cellId";

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    MyTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellId];

    if (!cell) {

        cell = [[MyTableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellId];

    }

    

    GroupModel *group =self.grupArray[indexPath.section];

    

    PersonModel *friend = group.friedns[indexPath.row];

    

    cell.models = friend;

    

    return cell;

    

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    //不要在这个方法中直接创建一个uivew对象返回。因为这样无法实现重用

    GroupModel *group =self.grupArray[section];

    

    MyHeadView *headView = [MyHeadViewheaderViewWithTableView:tableView];

    

    //创建UITableViewHeaderFooterView

//    UITableViewHeaderFooterView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellId];

//    if (headView == nil) {

//        headView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:cellId];

//    }

    

    headView.models = group;

    headView.delegate =self;

    headView.tag = section;

    

    //在刚刚创建好的header  view中获取的header view的frame都是0

    

    return headView;

    

    

}

- (void)headViewDidClickTitleButton:(MyHeadView *)headView

{

    NSIndexSet *idxSet = [NSIndexSetindexSetWithIndex:headView.tag];

    [self.tableViewreloadSections:idxSet withRowAnimation:UITableViewRowAnimationFade];

}

@end


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表