iOS 关于扣扣列表界面随笔

作者: Wo的小名叫单纯 | 来源:发表于2016-06-29 16:03 被阅读88次

首先我们先写关于APPdelegate.m里面的

首先引入你的viewController

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
    self.window.rootViewController = naVC;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

回到你的viewcontroller.h
创建一个UITableView,并且设置他的代理
以下是.h里面的内容

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>


@property(nonatomic,strong)UITableView *tableView;


@end

创建完tableView去 .m 文件中去实现
首先创建四个数组(在这里数组随意创建,最少两个,我个人创建了四个,因为分了三种),再加一个NSMutableDictionary

NSArray *rowArray;
    NSArray *rowArray1;
    NSArray *rowArray2;
    NSArray *sectionArray;
    
    NSMutableDictionary *showDic;//用来判断分组展开与收缩

好,我们去实现ViewDidLoad里面的东西

//设置tableview为全局
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    
    //为tableView设置代理
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    //添加tableView
    [self.view addSubview:_tableView];
    
    sectionArray = [NSArray arrayWithObjects:@"火影忍者",@"啦啦啦德玛西亚",@"七龙珠", nil];
    rowArray = [NSArray arrayWithObjects:@"小伦",@"信爷",@"嘉文",@"蕾娜", nil];
    rowArray1 = [NSArray arrayWithObjects:@"鸣人",@"佐助",@"宇智波鼬",@"蝎", nil];
    rowArray2 = [NSArray arrayWithObjects:@"孙悟空",@"达尔",@"孙悟饭",@"特兰克斯", nil];

好了,写完这些后让我们去实现需要的各种方法吧

//定义section个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return sectionArray.count;
}

//每个section里面有多少cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return rowArray.count;
}

//实现cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if(cell == NULL){
        
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        
        cell.selectionStyle = UITableViewRowAnimationNone;
        cell.separatorInset = UIEdgeInsetsZero;
        cell.clipsToBounds = YES;
    }
    
    if(indexPath.section == 0){
        cell.textLabel.text = rowArray1[indexPath.row];
    }else if (indexPath.section == 1){
        cell.textLabel.text = rowArray[indexPath.row];
    }else if (indexPath.section == 2){
        cell.textLabel.text = rowArray2[indexPath.row];
    }
    
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textColor = [UIColor redColor];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([showDic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.section]]){
        return 44;
    }
    return 0;
}

//section头部高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    
    UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 300, 20)];
    sectionLabel.text = [NSString stringWithFormat:@"%@",sectionArray[section]];
    sectionLabel.font = [UIFont systemFontOfSize:18];
    
    sectionLabel.textColor = [UIColor blackColor];
    [header addSubview:sectionLabel];
    
    //单机 recognizer,收缩分组cell
    header.tag = section;
    
    UITapGestureRecognizer *singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
    singleRecognizer.numberOfTapsRequired = 1;
    [singleRecognizer setNumberOfTouchesRequired:1];
    [header addGestureRecognizer:singleRecognizer];
    
    return header;
}


-(void)singleTap:(UITapGestureRecognizer *)sender
{
    NSInteger didSection = sender.view.tag;
    
    if(!showDic){
        showDic = [[NSMutableDictionary alloc] init];
    }
    
    NSString *key = [NSString stringWithFormat:@"%ld",didSection];
    
    if(![showDic objectForKey:key]){
        [showDic setObject:@"1" forKey:key];
        
    }else{
        [showDic removeObjectForKey:key];
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:didSection] withRowAnimation:(UITableViewRowAnimationFade)];
}

相关文章

网友评论

本文标题:iOS 关于扣扣列表界面随笔

本文链接:https://www.haomeiwen.com/subject/yxrxjttx.html