码迷,mamicode.com
首页 > 其他好文 > 详细

UITabelView(一)

时间:2016-02-29 21:42:48      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

  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

 

UITabelView(一)

标签:

原文地址:http://www.cnblogs.com/DevinSMR/p/5228766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!