iOS 关于扣扣列表界面随笔

首先我们先写关于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)];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,554评论 1 14
  • 最近一朋友正准备跳槽,就从各处搜索整理一些基础,便于朋友复习,也便于自己复习查看. 1. 回答person的ret...
    smile丽语阅读 1,811评论 0 7
  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 3,726评论 2 7
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 4,775评论 1 9
  • 总是假装自己很努力的样子,其实你一点都不努力。
    瑰云阅读 142评论 0 0