1、UITableView
API文档总结:




 
 
 1.UITableView的父类时,UIScrollView,所以它是可以滚动的,但是只能在竖直方向滚动.

2.UITableView是iOS中提供的用来以列表的形式展示数据,但是只有一列.

    
3.UITableView可以由多个分组构成(section),每个分组下可以有很多行(row),分组和行的下标都是从零开始.(eg:sention
班级分组 row 同学所在排数)

    
4.UITbleview可以有两种样式(Plain)和(Grouped)样式,一旦给tableView指定了样式之后就不能修改了;

    
5.UITableView很多方法都和NSIndexPath有关,NSIndexPath中存储的是当前你要使用的单元格(cell)所在分区的下标和所在分区中行的下标
   1.创建控件
   
UITableView
*tableView =
[[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style: UITableViewStylePlain
];
 
 
 
  2.配置属性
 
  2.1配置单元格(cell)的高
   
tableView.rowHeight = 100;

   
 
  2.2配置cell分割线的颜色
   
tableView.separatorColor = [UIColor redColor];

   
 
  2.3配置cell
分割线的样式
   
tableView.separatorStyle =   
UITableViewCellSeparatorStyleSingleLine;

//UITableViewCellSeparatorStyleNone,

//UITableViewCellSeparatorStyleSingleLine,

//UITableViewCellSeparatorStyleSingleLineEtched

2.4.1配置tableView的表头(表头很重要,经常用来做轮播图)
//   
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,
kScreenWidth, 50)];
//   
headerView.backgroundColor = [UIColor redColor];

   

   
  2.4.2.将准备的视图赋值给tableView的表头视图属性
//   
tableView.tableHeaderView = headerView;
 
   
2.5配置tableView表尾
//   
UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 508,
kScreenWidth, 40)];
//   
footView.backgroundColor = [UIColor cyanColor];
//   
tableView.tableFooterView = footView;

   
//快速去除多余单元格

//   
tableView.tableFooterView = [[UIView
alloc]init];

   

     2.6配置数据源代理





   
tableView.dataSource = self;
 
   2.7配置业务代理
   
tableView.delegate = self;
   
 
    
3.添加到父视图
   
[self.view addSubview:tableView];
 
    
4.释放
   
[tableView release];

   

   
//cell上有两个视图,一个是contentView
一个是accessoryView 而我们用到的cell属性
textLabel,detailTextLabel,imageView这些视图都是在contentView上,所以当我们自定义的控件放到contentView上
}
————————————————————————————————————
#pragma mark
tableView的数据源代理方法



 配置分区个数
-
(NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView{

 
 
  根据数组元素的格式确定分区个数

//   
return self.bigArray.count;

 
  
    
根据字典键值对的个数确定分区的个数
   
return
self.dictionary.count;

}


 
返回每个分区对应的行数

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{

   

   
  根据分区的下标找出大数组中小数组中元素

//   
return [self.bigArray[section]count];

   

   
//第一步:根据分区下的下标取出self.orderkey数组中存放的key值

NSString
*key =
self.orderkey[section];

   

   
//第二步:使用key取出字典中的value值(数组)并取出数组的元素个数

//   
[self.dictionary objectForKey:key];

   
return 
[self.dictionary[key]count];

}
——————————————————————————————

//在这个方法中创建单元格并对单元格配置数据

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   

   
// UITableViewCell
单元格视图  继承自UIView 用来展示数据

//   
UITableViewCell *cell = [[[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:nil]autorelease];

#pragma mark cell的重用
   
//cell的重用是为了避免反复消耗资源,耗能达到节省内存的作业

//cell重用的步骤

//1.创建一个重用标识(重用ID),这个标识在cell创建的时候使用,也在从重用池取cell时判断使用

//面试题:使用static修饰重用标识的字符串变量,可以使其只被初始化一次,提高一下重用效率

static 
NSString
*identfier 
= @"cell";

   

   
//2.当需要一个单元格(cell)时,先去"重用池"中根据"重用ID"去查找有没有可以使用单元格

//如果有:直接从重用池中拿出来使用(修改单元格上的数据)

//如果没有:此时只能直接创建一个新的单元格

//tableView就是当前代理的委托人

UITableViewCell
*cell =
[tableView dequeueReusableCellWithIdentifier:identfier];

//3.判断是否成功取到可重用的cell

if (cell == nil) {// !cell 
一个意思

       
NSLog(@"=========");

       
cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1)
reuseIdentifier:identfier]autorelease];
————————————————————————————————



 
 1.给cell的imageView属性赋值

   
cell.imageView.image = [UIImage imageNamed: @"11"];

   
//2.给cell的textLabel赋上文本内容

   
cell.textLabel.text = [NSString
stringWithFormat:@"%ld--%ld",indexPath.section,indexPath.row];

//3.给cell的detailLabel 赋上文本内容

   
cell.detailTextLabel.text = @"邓丽君";

   

    //4.设置cell 的右边界辅助视图

   
cell.accessoryType  =
UITableViewCellAccessoryDisclosureIndicator;

   
//   
cell.accessoryView = [[[UISwit ch alloc]init]autorelease];

   
//自定义辅助视图

       
UIView *accessoryView =[[UIView alloc]initWithFrame:CGRectMake(0,
0, 40, 20)];

       
accessoryView.backgroundColor = [UIColor redColor];

       
cell.accessoryView = accessoryView;

   

//       
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(300, 0, 20,
cell.frame.size.height)];

//       
view.backgroundColor = [UIColor orangeColor];

       
[cell addSubview:view];
   
}
——————————————————————————————



//配置每个分区的页眉,叫区头也可以

- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section{
//   
if (0 == section) {
//       
return @"L";
//   
}else if (1 == section){
//       
return @"W";
//   
}else{
//       
return @"Z";
//   
}

   
//根据排好序的数组返回区头

   
return
self.orderkey[section];

}
//配置分区的右侧的分区索引,区头的顺序和数组中的元素一致

- (NSArray *)sectionIndexTitlesForTableView:(UITableView
*)tableView{
//   
return @[@"F",@"S",@"G"];

   

   
//根据排好序的数组设置右侧分区索引

 return self.orderkey; 
 
}
//配置每个分区的页脚,叫区尾也可以

- (NSString *)tableView:(UITableView *)tableView
titleForFooterInSection:(NSInteger)section{

   
return
@[@"f",@"s",@"l"][section];

}

- (void)didReceiveMemoryWarning {

   
[super
didReceiveMemoryWarning];

// Dispose of any
resources that can be recreated.

}
————————————————————————————————
#define
kScreenWidth     [UIScreen
mainScreen].bounds.size.width

#import "DetailViewController.h"

@interface
RootViewController
()<</span>UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSArray
*bigArray;
@property(nonatomic,retain)NSDictionary
*dictionary;
@property(nonatomic,retain)NSArray
*orderkey;
@end

@implementation
RootViewController

- (void)dealloc{

self.bigArray
=
nil;

self.dictionary
=
nil;

self.orderkey
=
nil;

[super
dealloc];

}
   
NSArray *lArr = @[@"李白",@"李妹",@"李舒缓",@"梨花"];

   
NSArray
*zArr =
@[@"张波",@"涨姿势",@"张紫琴"];

   
NSArray
*wArr =
@[@"王小波",@"王伟",@"王小二"];
//   
self.bigArray = @[lArr,zArr,wArr];

   

   
self.dictionary = @{@"L":lArr,@"Z":zArr,@"W":wArr};

   
//取出字典中所有的key值

NSArray
*keys =
self.dictionary.allKeys;

   
//将排好序的key值赋值给self.orderkey

  
self.orderkey = [keys sortedArrayUsingSelector:@selector(compare:)];
——————————————————————————————————

#pragma mark
tableView的业务代理方法
//返回cell的行高

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{

   
if (0 != indexPath.row % 2) {

       
return
80;

   
}else{

   
return
200;

   
}

}
//触发时机
:点击cell的时候会触发,所以要完成点击cell跳转到其他页面,方法实现写在这个方法中

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//   
NSLog(@"%ld --
%ld",indexPath.section,indexPath.row);

   
DetailViewController
*detail =
[[DetailViewController
alloc]init];

   
detail.textString = [NSString stringWithFormat:@"%ld -- %ld",indexPath.section,indexPath.row];
   
[self.navigationController
pushViewController:detail
animated:YES];
   
[detail release];
}


//触发时机:点击一个cell之后,在点击其他的cell时触发

- (void)tableView:(UITableView *)tableView
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0){

//NSLog(@"%ld --
%ld",indexPath.section,indexPath.row);  
}
#pragma mark
从数组中取数据展示
   
//1.先取出大数组中的元素
   
NSArray
*nameArray =
self.bigArray[indexPath.section];

   
//2.从nameArray取出名字

NSString
*name =
nameArray[indexPath.row];
//   
NSLog(@"%@",name);

   

   
//3.将给出来的名字赋值给cell.textLabel.text

   
cell.textLabel.text = name;
   
cell.imageView.image = [UIImage imageNamed:@"11"];

   

#pragma mark
从字典中取出数据显示
   
//1.根据分区下标取出self.order
中的元素

   
NSArray
*key =
self.orderkey[indexPath.section];

   

   
//2.取出key值在字典中对应的数字

NSArray
*names =
self.dictionary[key];

   

   
//3.根据行下标取出数组中的名字,并赋值给textLabel.text

   
cell.textLabel.text =
names[indexPath.row];
最终效果图:



——————————————————————————————



 
 欢迎学习本文,未经博主许可禁止转载!

UITableViewBase&nbsp;UI_09的更多相关文章

随机推荐

  1. Java HashMap的扩容

    最近博主参加面试,发现自己对于Java的HashMap的扩容过程理解不足,故最近在此进行总结. 首先说明博主德Java为1.8版本 HashMap中的变量 首先要了解HashMap的扩容过程,我们就得 ...

  2. 让ckplayer支持m3u8格式的播放

    一 ckplayer官网下载. 直接解压到本地,注意配置里面的默认文件路径,最好是放在服务器的根目录下,这样就可以去访问外链接的资源,官网也是这么推荐的,我这里就直接用node开启一个小服务器. 二 ...

  3. Springboot项目maven多模块拆分

    多模块拆分的必要性 使用Java技术开发的工程项目,无论是数据处理系统还是Web网站,随着项目的不断发展,需求的不断细化与添加,工程项目中的代码越来越多,包结构也越来越复杂这时候工程的进展就会遇到各种 ...

  4. django.db.utils.ProgrammingError: 1146 的解决办法

    在models中设置完数据库相关的东西后执行命令 python manage.py makemigrations 此处无错误 再次执行 python manage.py migrate 发生报错 错误 ...

  5. JAVA局部内部类

    在刚刚学到的android开发中了解到Button的onClick是通过局部内部类的方式实现的,具体的原理我以后再去了解,只是遇到问题总是想知道为什么,不要告诉我这是规则,死记住就可以了. 问题是局部 ...

  6. python解析json文件之简介

    一.JSON简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition ...

  7. 牛客网编程练习之PAT乙级(Basic Level):1041 说反话

    直接分隔取反即可 AC代码: import java.util.Scanner; /** * @author CC11001100 */ public class Main { public stat ...

  8. PHP MySQL Delete

    DELETE 语句用于从数据库表中删除行. 删除数据库中的数据 DELETE FROM 语句用于从数据库表中删除记录. 语法 DELETE FROM table_name WHERE some_col ...

  9. 安卓onTextChanged参数解释及实现EditText字数监听 Editable使用

    原作者部分修改部分 补充部分 补充部分2 补充部分3 补充部分4 Editable 尊重原作者:此篇文章是借鉴原作者地址 的博文 并进行修改和增加补充说明,我只是补充和修改: 我感觉这篇文章经过我的补 ...

  10. Android TV开发总结(四)通过RecycleView构建一个TV app列表页(仿腾讯视频TV版)

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52854131 前言:昨晚看锤子手 ...