UITableView 初探

初始化

// 一般给屏幕大小的frame
 UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];

属性

// 设置代理和数据源(tableView的很多设置和方法都需要在这两个协议中实现)
    tableView.delegate = self;
    tableView.dataSource = self;
// 设置整个tableView的表头
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    headerView.backgroundColor = [UIColor greenColor];
    tableView.tableHeaderView = headerView;
    [headerView release];
// 设置整个tableView的表尾
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    footerView.backgroundColor = [UIColor brownColor];
    tableView.tableFooterView = footerView;
    [footerView release];

协议方法

#pragma mark -- dataSource 必须实现的2个方法

// 返回 每一个分区各自的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

// 返回 索引处(哪一个分区的哪一行) 的每一个 cell(单元格)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
     UITableViewCell重用
     需要一个重用的集合 作用:把滑出屏幕的cell(完全消失在屏幕上时) 放入这个重用集合(备用)
     当屏幕下方需要新的cell进行展示的时候开始重用
     方式是 首先 系统会先去 重用集合 中找 看有没有cell 可以重新使用 如果有 就直接使用 如果没有 就创建一个出来进行使用
     */

    // 标识符
    // 可以区分每一种cell的样式
    static NSString *identifier = @"MyCell"; // 防止多次创建释放的过程

    // 去重用集合中 按标识符 寻找对应的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        // 去创建新的cell
        // 释放cell的时候 只有创建出来的才需要去释放 从集合中取出来 不用释放
        cell = [[[UITableViewCell alloc] initWithStyle:
                 UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
        static int number = 0;
        number++;
        NSLog(@"%d",number);

    }
    // 设置单元格上的标题
    cell.textLabel.text = @"哈哈";
    cell.detailTextLabel.text = @"呵呵";
    // 设置单元格上的图片
    cell.imageView.image = [UIImage imageNamed:@"4.jpg"];
    // 设置辅助按钮
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    return cell;
}
// 设置每个分区的表头和表尾
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    headView.backgroundColor = [UIColor grayColor];
    return [headView autorelease];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    footView.backgroundColor = [UIColor purpleColor];
    return [footView autorelease];
}

// 设置分区表头 和 表尾 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20;
}


// 设置每个分区的每一行 的 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

// 设置分区表头 的 标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return @"A";
            break;
        case 1:
            return @"B";
            break;
        case 2:
            return @"C";
            break;
        default:
            return @"#";
            break;
    }

}

// 设置tableView右边 标题小按钮
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSArray *sectionIndexTitles = @[@"A",@"B",@"C"];
    return sectionIndexTitles;
}

tableView和数据

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUpData];
    [self addTableView];
}

// 数据的处理
- (void)setUpData
{
    // 拿到路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"TableViewPlist" ofType:@"plist"];

    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];

// 把Value中的字典 转化为数据模型
// 创建装完model的字典
    self.dataDic = [NSMutableDictionary dictionary];

// 遍历字典
// 取出所用key
    NSArray *keys = [dic allKeys];
        for (int i = 0; i < keys.count; i++) {
        // 取出每一个key
        // 用每一个key取出 对应的Value
        NSString *key = keys[i];
        NSArray *value = dic[key];
        // 创建临时数组 保存每一个赋值完成的model
        NSMutableArray * tempArray = [NSMutableArray array];
        // 遍历每一个value
        for (NSDictionary *oneDic in value) {
            // 给model赋值
            // 创建model
            CellModel *model = [[CellModel alloc] init];
            // 给我一个字典 还你一个赋值完成的model
            [model setValuesForKeysWithDictionary:oneDic];
            // 把model装进临时数组中
            [tempArray addObject:model];
            // 释放
            [model release];
        }
        // 重新构建 字典的 键值对
        [self.dataDic setObject:tempArray forKey:key];
    }

}

- (void)addTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"MyCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:
                 UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];

    }
    // 赋值位置
    NSArray *keys = self.dataDic.allKeys;
    NSString *key = keys[indexPath.section];
    NSArray *values = self.dataDic[key];
    CellModel *model = values[indexPath.row];

    // 赋值cell
    cell.textLabel.text = model.title;
    cell.imageView.image = [UIImage imageNamed:model.imageName];


    return cell;
}

// 点击跳转的方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"sdf");
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *keys = self.dataDic.allKeys;
    NSString *key = keys[section];
    NSArray *value = self.dataDic[key];

    return [value count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值