UITableView的基本使用(一)


#import "RootViewController.h"

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong,nonatomic) NSMutableDictionary *dictionary;

@property (strong,nonatomic) NSMutableArray *array;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"张壹",@"张贰",@"张叁",@"张肆",@"张伍", nil];
//    NSArray *array2 = [NSArray arrayWithObjects:@"", nil]
    self.array = array1;

    //创建一个新的TableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    //设置行高
    tableView.rowHeight = 100;
    //设置分割线的颜色
    tableView.separatorColor = [UIColor redColor];
    //设置分割线的样式
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    //设置一下顶部视图和底部视图
    //顶部视图
    UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavBar_44.png"]];
    tableView.tableHeaderView = imageView1;
    //底部
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NavBar_44@2x.png"]];
    tableView.tableFooterView = imageView2;
    [self.view addSubview:tableView];
    
    //设置代理
    tableView.dataSource = self;
    tableView.delegate = self;
    
    //cell的重用机制
     /*
      总结:
       重用机制描述:使用cell的重用机制大大的减少了内存的占用,当在处理大数据显示的时候起到很好的作用
       当前屏幕根据尺寸显示相应数量的cell的单元格,所以看不到的,不用创建,只需要创建屏幕显示的就可以了。
      具体的介绍:
      屏幕根据尺寸显示相应的cell的单元格个数,假如根据屏幕尺寸显示了六个cell的单元格,当划动视图的时候上面的消失在视线内的cell的单元格,不会销毁,放到重用池当中。当下一个将要显示在视线当中的cell要出现的时候,从重用池中取出这个cell,赋值相应要显示的数据,载入视图出现在视线中。如果重用池里面没有cell需要创建一个新的。
      具体实现过程:
         有两种方法
        第一种:1:在viewDidLoad先创建一个cell的单元格,赋值标识,放置在重用池当中。
               2:在cell的设置样式代理方法里面实现,根据标识查找第一步创建的cell。然后调用赋值数据显示在视线俩面
        第二种:1:在cell的设置样式代理方法里面实现,首先根据标识查询是否存在响应的cell单元格。
               2:如果存在世界调用赋值,显示在视线内,如果不存在创建一个新的cell调用显示在视线内。
         */
    //具体实现代码:第一种
    //1:创建一个新的cell
    //第一步:注册一个cell【写在创建完tableView之后,需要注册cell】(当重用池里面没有cell的时候,才会注册cell);
    //参数1:是一个类 (当重用池里面没有cell的时候开始创建cell类)
    //参数2:给重用池一个标识
//    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseCell"];
    
    
    
    
    
    
    
}

#pragma mark - UITableViewDataSource 代理实现的方法

//代理方法,每个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.array.count;
}

//代理方法,每行cell的样式设置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //创建一个新的cell 如果想要自定副标题,需要使用非系统默认的cell样式
//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"subtitleCell"];
    //第一种:2
    //根据相应的cell标识查找重用池里面的cell
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseCell"];
    
    //第二种:1:查找重用池里面是否存在cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseCell"];
    //2:判断是否存在 如果不存在直接创建一个。
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"reuseCell"];
    }
    
    cell.imageView.image = [UIImage imageNamed:@"NavBtnLeft@2x"];
    cell.textLabel.text =[self.array objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = @"姓张";
    return cell;
}
//设置有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}

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

   return @"老乡";

}
//设置分区的底部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    return @"老家";
    
}
//设置索引 出现在屏幕右侧的索引值
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return @[@"A",@"B",@"C"];
}

#pragma mark - UITableViewDelegate 代理的方法

//使用代理方法,设置每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    //设置每一区的第一行的高度
    if (indexPath.row == 0) {
        return 100;
    }
    return 50;
}

//使用代理设置每一区的头部标题高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 100;
}

//使用代理设置每一区的尾部的标题高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 100;
}





转载于:https://my.oschina.net/rdqblogs/blog/626129

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值