[TwistedFate]UITableView表视图

表示图TableView

表视图UITableView,iOS中最重要的视图,随处可⻅见。表视图通常⽤用来管理⼀一组具有相同数据结构的数据.
步骤与之前一样,先创建根视图,因为tableView的实现方法比较多,可以创建一个addTableView方法,然后在视图控制器的viewDidLoad中调用.

初始化以及代理设置

-  (void)addTableView{
//  初始化
UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];

//  设置代理和数据源
//  数据源负责给tableView提供数据
//  需要实现两个必须实现的方法
tableView.delegate = self;
tableView.dataSource = self;

//  设置整个TableView 的表头和表尾

//  只有高度能改变
UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 200)];

headView.backgroundColor = [UIColor cyanColor];

tableView.tableHeaderView = headView;

[headView release];

}

//  X轴与高度能改变
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(50, 20, 100, 100)];
footerView.backgroundColor = [UIColor greenColor];
tableView.tableFooterView = footerView;
[footerView release];
// 显示视图
[self.view addSubview:tableView];

// 释放
[tableView release];

协议方法的实现

//  返回分区数
-  (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 5;

}

//  返回每个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    // 五行
    return 5;
}

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

    return 100;
}

//  返回的是索引处的每一个cell(单元格)
//  索引(分区 ---- > 行)
// 返回的是索引(分区 -- > 行)处的每一个cell(单元格)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    // 创建UITableViewCell
    // 标示符区分每一种cell的样式
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];

    // 设置标题
    cell.textLabel.text = @"哈哈";
    cell.detailTextLabel.text = @"呵呵";

    // 设置cell上的图片
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",indexPath.row + 1]];

    // 设置辅助按钮
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    return [cell autorelease];
}

//  设置TableView右边的小标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return @[@"a",@"b",@"c"];
}


设置每个分区的表头和表尾

//  表头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];

    headView.backgroundColor = [UIColor redColor];

    return [headView autorelease];

}

//  表尾
- (UIView *) :(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView *footview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];

    footview.backgroundColor = [UIColor grayColor];

    return [footview autorelease];

}

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

    return 100;
}

//  设置分区表头的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    NSArray *array = @[@"X", @"Y", @"Z", @"D", @"A"];
    return array[section];
}

cell的重用机制

需要一个重用的集合 作用:把划出屏幕的cell(完全消失 在屏幕上)放入这个重用集合(备用) 当屏幕下方 需要新的cell进行展示的时候 开始重用的方式 系统先去重用的集合中找 看有没有cell可以重新使用

所以cell的部分应该如下这么写

 static  NSString *identifier = @"MyCell";
    //按标识符 寻找对应的cell

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {//创建新的cell 只有创建 才需要释放

        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyCell"] autorelease];

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值