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

来源:互联网 发布:矩阵分配律 编辑:程序博客网 时间:2024/05/01 10:21

之前的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
原创粉丝点击