Snail—UI学习之UITableView之分组显示

之前的demo都是一个分组显示数据的

这次我们用的是带有分组的tableView

#import "WJJRootViewController.h"

@interface WJJRootViewController (){
    UITableView * _tableView;
    NSMutableArray * _dataArray;
}

@end

@implementation WJJRootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self createDataSource];
}


- (void)createDataSource{
    _dataArray = [[NSMutableArray alloc] init];
    //先把工程下 所有的plist路径获取到 装到数组里面
    NSArray * plistPath = [[NSBundle mainBundle] pathsForResourcesOfType:@"plist" inDirectory:@""];
    //遍历这个数组 把系统的plist剔除
    for (NSString * pathString in plistPath) {
        //如果这个路径 是系统的plist路径 略过
        if ([pathString hasSuffix:@"Info.plist"]) {
            continue;
        }
        NSArray * plistArray = [[NSArray alloc] initWithContentsOfFile:pathString];
        [_dataArray addObject:plistArray];
        
    }
    [self createTableView];
}

- (void)createTableView{
    
    //tableView的风格是分组的
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                              style:UITableViewStyleGrouped];
    
    //设置代理和数据源代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
}

#pragma mark --UITableViewDelegate--
//tableView是分组类型的  先设置组的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}

//设置每组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //返回数据源里 与 section相对应的 数组的元素个数
    return [[_dataArray objectAtIndex:section] count];
}

//cell的代理方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [_tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
    }
    NSDictionary * dict = [_dataArray[indexPath.section] objectAtIndex:indexPath.row];
    [cell.imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",[dict objectForKey:@"imageName"]]]];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@",[dict objectForKey:@"imageInfo"]]];
    [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@",[dict objectForKey:@"imageInfo"]]];
    
    return cell;
}

//设置头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    //这里的数据是我们自己写的
    NSArray * titleArray = @[@"圣斗士",@"海贼王",@"火影忍者",@"妹子们"];
    return titleArray[section];
}

//设置索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return @[@"圣",@"海",@"火",@"妹"];
}

//返回行标题的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//返回行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值