zoukankan      html  css  js  c++  java
  • UITabelView(一)

      1 #import "RootViewController.h"
      2 
      3 @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
      4 @property(nonatomic,strong)NSArray *dataSourceArray;
      5 @end
      6 
      7 @implementation RootViewController
      8 
      9 - (void)viewDidLoad {
     10     [super viewDidLoad];
     11     // Do any additional setup after loading the view.
     12     
     13     self.dataSourceArray = [NSArray arrayWithObjects:@"李曼",@"刘梦君",@"张秀洁",@"朱磊",@"孟德峰",@"张静",@"温哲", nil];
     14     
     15     //创建UITbaleView
     16     UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
     17    
     18     
     19     
     20 //    //设置一些常用的属性
     21 //    //设置行高
     22 //    tableView.rowHeight = 100;
     23 //    //设置分割线的颜色
     24 //    tableView.separatorColor = [UIColor redColor];
     25 //    //设置分割线的样式
     26 //    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
     27 //    //设置头部
     28 //    tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0,0, 100, 100)];
     29 //    tableView.tableHeaderView.backgroundColor = [UIColor cyanColor];
     30 //    
     31 //    //设置尾部(要想显示出想要的那种分割开来的效果必须给tabelView设置代理)
     32 //    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 100, 100, 100)];
     33 //    tableView.tableFooterView.backgroundColor = [UIColor brownColor];
     34     
     35     
     36     [self.view addSubview:tableView];
     37     
     38     
     39 #pragma mark - 设置代理(这是tableView非常重要的内容)
     40     //设置显示数据的代理(用于处理数据相关的内容)
     41     tableView.dataSource = self;
     42     //设置自身的代理(设置用于显示视图相关的内容)
     43     tableView.delegate = self;
     44     
     45 #pragma mark - cell的重用机制(非常重要)
     46     
     47     /*
     48      重用机制的流程:
     49      1.当一个cell被划出屏幕的时候,这个cell会被放置到重用池内
     50      2.当tableView要显示一个cell的时候先去重用池里去找
     51      3.当在重用池中找不到cell,此时需要新建cell
     52      4.如果找到cell,取出并赋值
     53      */
     54     //要实现重用有两种方式实现,先讲第一种
     55 #warning 第一种重用方式
     56     //第一种的第一步:注册一个cell[创建完tableView之后,需要注册cell](当重用池中没有cell的时候才需要注册cell)
     57     //参数1.是一个类,(当重用池中没有cell的时候开始创建cell类) 参数2:给重用池一个重用标识
     58     [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reusedCell"];
     59     
     60 }
     61 #pragma mark - UITableViewDataSource代理方法
     62 //设置tableView每一个区里边有多少行
     63 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     64 {
     65     //了解:这个方法走的次数跟Xcode版本有关
     66     NSLog(@"numberOfRowsInSection:%s",__FUNCTION__);
     67     return self.dataSourceArray.count;
     68 }
     69 //设置每一个区的每一行的cell的样式
     70 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     71 {
     72     //查看调用了几次这个方法
     73     NSLog(@"cellForRowAtIndexPath:%s",__FUNCTION__);
     74     
     75     
     76     
     77     //第二步:根据重用标识去取重用的cell在这个地方使用
     78 //    //系统方法中封装了根据重用标识去取相关的cell
     79 //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusedCell"];
     80 //    //对cell进行一个内容的设置
     81 //    cell.imageView.image = [UIImage imageNamed:@"1.jpeg"];
     82 //    cell.textLabel.text = [NSString stringWithFormat:@"row:%ld",indexPath.row];
     83     
     84 #warning 第二种重用方式
     85     //第一步:从重用池里去取相应的cell
     86     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusableCell"];
     87     //第二步:如果重用池中没有重用的cell,需要创建cell
     88     if (cell == nil) {
     89         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reusableCell"];
     90     }
     91     //设置cell的内容
     92     cell.imageView.image = [UIImage imageNamed:@"1.jpeg"];
     93     cell.textLabel.text = [NSString stringWithFormat:@"row:%ld",indexPath.row];
     94     cell.textLabel.text = [self.dataSourceArray objectAtIndex:indexPath.row];
     95     
     96     /*
     97     //创建cell
     98     //UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
     99      //2.改为支持显示副标题的类型的cell
    100     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    101     //在这个代理方法里设置具体的cell的内容
    102     //设置UITableViewCell中重要样式的三个属性
    103     //cell.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpeg"]];
    104     cell.imageView.image = [UIImage imageNamed:@"1.jpeg"];
    105     //设置标题
    106     cell.textLabel.text = @"朴智妍";
    107     //设置副标,想要设置UITableViewCell的副标题,必须修改UITableView的一个样式,因为默认的是无副标题的
    108     cell.detailTextLabel.text = @"美女";
    109     //返回cell
    110      */
    111     return cell;
    112 }
    113 //设置有几个分区,默认的是一个
    114 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    115 {
    116     return 2;
    117 }
    118 
    119 //设置头部的标题
    120 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    121 {
    122     return [NSString stringWithFormat:@"section:%ld",section];
    123      //return @"我们很嗨";
    124 }
    125 //设置尾部的标题
    126 -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    127 {
    128      return @"好好学习";
    129 }
    130 
    131 //设置右侧的索引
    132 -(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
    133 {
    134     return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"G",@"K",@"L",@"M",@"N",@"O",];
    135 
    136 }
    137 #pragma mark  - UITableViewDelegate 的代理方法
    138 //设置每一行的高度
    139 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    140 {
    141     //设置的是:每一个区里第一行的高度
    142     if (indexPath.row == 0) {
    143         return 100;
    144     }
    145     
    146     return 50;
    147 }
    148 //设置每一个区头部的高度
    149 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    150 {
    151     return 100;
    152 }
    153 //设置每一个区尾部的高度
    154 -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    155 {
    156     return 100;
    157 }
    158 @end
  • 相关阅读:
    SpringMvc+hibernate+easyui简单的权限管理系统
    使用Spring Security实现权限管理
    基于Spring Security2与 Ext 的权限管理设计与兑现
    Hibernate里面如何使用DetachedCriteriaCriteria 实现多条件分页查询
    mysql存储过程
    java学习路线与书籍推荐
    你真的懂wait、notify和notifyAll吗
    java中String对象的存储位置
    如何阅读Java源码
    Session概述(java)
  • 原文地址:https://www.cnblogs.com/DevinSMR/p/5228766.html
Copyright © 2011-2022 走看看