UI一揽子计划 9 (UITableView 、UITableView 、重用机制)

来源:互联网 发布:android 相机源码 编辑:程序博客网 时间:2024/06/05 04:34
一. UITableView

UITableView继承自UIScrollView,所以可以滚动
表视图的每⼀一条数据都是显示在UITableViewCell对象中
表视图可以分区显⽰示数据,每个分区称为⼀一个section,每⼀一⾏行称为row,编号都是从0开始

两个代理 :
@interface  UIViewController<UITableViewDataSource,UITableViewDelegate>
这两个方法必须实现
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath  
 
二. UITableView

UITableView中每⼀一个单元格,被称为⼀一个cell
(UITableViewCell)。 系统预置了4种(枚举)样式的cell。 不同样式的cell包含的控件有细微差别

三. 重用机制

UITableViewmutableSet来实现重⽤用功能
出屏幕的cell会被添加到mutableSet中,进⼊入屏幕的cell,先从set中 获取,如果获取不到,才创建⼀一个cell。在cell显⽰示之前,给cell赋上相应的内容。
cellreuseIdentifier是重用的关键。

四. 方法们
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView; //分区数
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section; //分区头标题
- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView; //右侧竖排索引

// 下面四个方法用来设置单元块的头和尾的高度和View
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section;
- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section;
- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section;

// 单元格的高度及选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
五. MVC模式的实现
(详见UITableView0004)
UITableView0001
     //创建并初始化一个UITableView
   /*
     UITableViewStylePlain  
不分区 表头标题可以固定到该分区的最后一行画出的时候改变
     UITableViewStyleGrouped 
分区 标题显示在
     */

   
UITableView *tableView = [[UITableViewalloc]initWithFrame:[UIScreenmainScreen].bounds style:(UITableViewStylePlain)];
//设置重要属性
   
// 一个数据源 一个代理的属性
    tableView.
dataSource= self;
    tableView.
delegate= self;
   
   
//设置表头和表尾
 
// 这个是整个tableView的表头和表尾
   
UIView *headView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,375,100)]; // 最好设置375
    headView.
backgroundColor= [UIColorgreenColor];
    tableView.
tableHeaderView= headView;
    [headView
release];
   
   
UIView *footView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100,100)];
    footView.
backgroundColor= [UIColorredColor];
    tableView.
tableFooterView= footView;
    [footView
release];
   
    [
self.viewaddSubview:tableView];

}

#pragma mark ---- datasource 两个必须实现的方法
//返回每个分区有多少行
- (
NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
   
return 15;
}
~~~~~返回有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
  return3;
}

~~~~返回每一行的样式

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
   
// 创建一个UITableViewCell并返回
   
// 四种样式 默认    reuseIdentifier标识符
   
/*
     //
详情标题
     cell.detailTextLabel.text = @"df";
     cell.textLabel.text = @"haha ";
     UITableViewCellStyleDefault  
子标题不显示
     UITableViewCellStyleSubtitle 
子标题在下面
     UITableViewCellStyleValue1
子标题在右面
     UITableViewCellStyleValue2
子标题压缩了 有图片不用这个
    
    */
 UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:(UITableViewCellStyleSubtitle)reuseIdentifier:@"CKCell"];
    cell.
imageView.image= [UIImageimageNamed:@"faye1"];
    cell.
detailTextLabel.text= @"df";
    cell.
textLabel.text= @"haha ";
   
// cell 右面按钮的样式  (四种样式   箭头 圈和箭头)
    cell.
accessoryType= UITableViewCellAccessoryDetailButton;
   
return [cell autorelease];
   
// 不用 release ?????????????
}
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
   
NSLog(@"%ld", section);
   
// 返回 每个分区 表头的标题信息
  
// return @"A";
   
if (section == 0) {
       
return @"a";
    }
else if (section == 1) {
       
return @"b";
    } 
else {
       
return @"c";
    }
}
 (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
   
// tableView分区索引的标题们
   
return @[@"a",@"b",@"c"];
}
- (
void)didReceiveMemoryWarning {
    [
superdidReceiveMemoryWarning];
   
// Dispose of any resources that can be recreated.
}
~~~~~~~设置每个分区的表头跟表尾 这个使用代理方法来实现的
- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
   
UIView *headView = [[[UIViewalloc]initWithFrame:CGRectMake(0,0,375,5)]autorelease]; // 最好设置375
    headView.
backgroundColor= [UIColororangeColor];
   
return  headView;

}

~~~~~返回每个分区的表头高度
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
   
return 50;
}

~~~~~表尾如上.......
- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
   
return 50;
}
- (
UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
   
UIView *footView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,375,5)];
    footView.
backgroundColor= [UIColorgrayColor];
   return  [footViewautorelease];  
}
~~~~~~~~恶心版Plus
//返回的是每个cell的高度
- (
CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
   
return 100;
}

 上午小结
 
 *  1.
创建的时候有两种样式
    2.
有两个代理  datasourse(两个方法必须执行必须实现的
                                ->1.
分区有多少行,
                                ->2.
每行都是什么样式的cell
                                <-
有几个分区
                                <-
表头表尾的标题
                            )
                  UItableDelegate (//cell的高度)  要释放

UITableView0002
- (void)dealloc
{
    [
_dataArrayrelease];
    [
superdealloc];
}

- (
void)viewDidLoad {
    [
superviewDidLoad];
    [
selfaddTableView];
   
// Do any additional setup after loading the view.
}

~~~~~~创建假数据
- (void)setUpDate
{
   
self.dataArray= @[@"思聪",@"小芳",@"",@"",@""];
}

//创建tableView
- (
void)addTableView
{
   
UITableView *tableView = [[UITableViewalloc]initWithFrame:[UIScreenmainScreen].boundsstyle:(UITableViewStylePlain)];
   
// 设置代理   数据源
    tableView.
delegate= self;
    tableView.
dataSource= self;
    [
self.viewaddSubview:tableView];
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    [
selfsetUpDate];
  
// NSLog(@"我是第%ld分区的", section);
   
// 返回每个分区有多少行
   
return self.dataArray.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
#pragma mark ---以后禁止这么写
   
// NSIndexPath有行和分区的属性
//    NSLog(@"我是%ld分区的%ld", indexPath.section, indexPath.row);
//    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
   
//利用 行数 indexpath.row 的变化 按照下标 动态的取出数据
//    cell.textLabel.text = self.dataArray[indexPath.row];
//    return cell;
   
   
   
#pragma mark ---重用机制
   /**
     * 
重用机制 
            1.
首先要知道屏幕显示几条数据 比如 5条数据
    
            2.
当向上滑的时候,有两个创建的选择先要去集合里面找,找不到就创建一个(第一次找的时候肯定没有,需要创建一个的)
    
            3.
当第一个cell完全划出屏幕的时候 这时候该cell变成未显示状态,然后扔进集合里面.
    
            4.
当下面再需要新出来的一个的时候,先去集合中找未显示状态的,有旧直接用,没有就去创建.
    
            5.
如此循环 终成正果
    
    
~~~~~~~~
可变集合中刚开始显示几个集合中就有几个(只不过是显示状态的最多这个集合中有几个?  显示的数量 + 1 .
     */

   

#pragma mark ---以后这么写
   
   
   
// 先写一个标识符 加上static就只初始化一次
  
static NSString *identifier = @"JJCell";
   
// 去集合里面取找 有的话就使用
   
   
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
   
~~~~~~下面进行判断如果没有就创建
   if (cell == nil) {
        cell = [[[
UITableViewCellalloc]initWithStyle:(UITableViewCellStyleSubtitle)reuseIdentifier:identifier]autorelease];
    }
   
~~~~~~对取出来的或者新创建的进行赋值(即改名字)
   
    cell.
textLabel.text= self.dataArray[indexPath.row];
   
return cell;
}

UITableView0003
- (void)setUpData
{
   
NSArray *groupD = [NSArrayarrayWithObjects:
                      
@"尚亚雷",
                      
@"李永乐",nil];
   
NSArray *groupC = [NSArrayarrayWithObjects:
                      
@"李云飞",
                      
@"莫亮萍",nil];
   
NSArray *groupM = [NSArrayarrayWithObjects:
                      
@"陈文博",
                      
@"孙永飞",
                      
@"王东旭",nil];
   
NSArray *groupN = [NSArrayarrayWithObjects:
                      
@"闫亚军",
                      
@"仝兴伟",
                      
@"代秋田",
                      
@"何亚楠",
                      
@"张宏宇",
                      
@"朱海明",
                      
@"杨松",nil];
   
NSArray *groupE = [NSArrayarrayWithObjects:
                      
@"于慧霞",
                      
@"庞仕山",
                      
@"陈岩岩",
                      
@"柳中帅",
                      
@"冯贵宾",
                      
@"杨秘",
                      
@"杨雪蕾",
                      
@"王强",
                      
@"薛石磊",
                      
@"王增双",
                      
@"裴小芳",
                      
@"毛军军",nil];
   
NSArray *groupK= [NSArrayarrayWithObjects:
                     
@"张国文",
                     
@"张莹莹",
                     
@"郑明",
                     
@"张莹莹",
                     
@"崔锴",
                     
@"蒲元凯",
                     
@"顾成辉",
                     
@"王向明",
                     
@"冯振玲",
                     
@"陈健健",
                     
@"夏炎",
                     
@"王昊",
                     
@"武自健",nil];
   
NSArray *groupA = [NSArrayarrayWithObjects:
                      
@"卢奕霖",
                      
@"王艺拓",
                      
@"李雨",
                      
@"刘新林",
                      
@"田菲菲",
                      
@"李琳冰",
                      
@"王岩",
                      
@"郭杰",
                      
@"杨淋淇",
                      
@"肖扬",
                      
@"黎一良",
                      
@"谷浩",
                      
@"孙昊坤",nil];
   
NSArray *groupZ = [NSArrayarrayWithObjects:
                      
@"赵枫俊",
                      
@"张明军",
                      
@"刘迪",
                      
@"聂银龙",
                      
@"张博",
                      
@"王珂",nil];
   
   
self.dataDic= @{@"A":groupA,@"Z":groupZ,@"C":groupC,@"D":groupD,@"K":groupK,@"E":groupE,@"M":groupM,@"N":groupN};
   
   
// 5. 取出所有的KEYs进行排序
   
   
NSArray *keys = [self.dataDicallKeys];
   
self.sortKeysArray= [keys sortedArrayUsingSelector:@selector(compare:)];
   
NSLog(@"%@",self.sortKeysArray);
   
   
}

- (
void)addTableView
{
   
// 1.
   
UITableView *tableView = [[UITableViewalloc]initWithFrame:[UIScreenmainScreen].boundsstyle:(UITableViewStylePlain)];
   
// 2.代理
    tableView.
delegate= self;
    tableView.
dataSource= self;
    [
self.viewaddSubview:tableView];
    [tableView
release];
}
// 3.多少行
- (
NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;
{
   
   
// 把对应的数组取出来
   
NSString *key = self.sortKeysArray[section];
   
NSArray *values = self.dataDic[key];
   
   
return values.count;
}


- (
UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
   
static NSString *identifier = @"JJCell";
   
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
   
if (cell == nil) {
        cell = [[[
UITableViewCellalloc]initWithStyle:(UITableViewCellStyleSubtitle)reuseIdentifier:identifier]autorelease];
    }
   
   
// 6.
   
// 显示数据     利用indexPath.section分区 取出key
   
NSString *key = self.sortKeysArray[indexPath.section];
   
// 利用 key 值来取出 每一个数组
   
NSArray *values = self.dataDic[key];
   
// 利用 indexPath.row 每个分区的行数 取出数组中的字符串
    cell.
textLabel.text= [values objectAtIndex:indexPath.row];
   
return cell;
}

// 4.返回分区数

-(
NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
   
// 有多少键值对就返回多少
   
return self.dataDic.count;
}
// 7.现加表头标题
- (
NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
   
// 用分区取出key
   
NSString *key = self.sortKeysArray[section];
   
return key;
}
// 8.右面的点击的 contentOFFSet 偏移量
- (
NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView
{
   
return self.sortKeysArray;
}

UITableView0004

UITableView.m文件
// 1.处理数据
- (
void)setUpData
{
   
// 读取文件夹中的数据文件plist文件
       
// 工程的目录  [NSBundle mainBundle]
   
NSString *path = [[NSBundlemainBundle]pathForResource:@"TableViewPlist"ofType:@"plist"];
   
// 将文件中的数据拿出来 放到字典里面
   
NSDictionary *plistDic = [NSDictionarydictionaryWithContentsOfFile:path];
//我从定义属性字典过来
   
// 循环外面初始化这个大字典
   
self.dataDic= [NSMutableDictionarydictionary];
//我是从CellModel过来的.
   // 遍历字典
   
// 取出所有的Key
   
NSArray *keys = [plistDic allKeys];
   
for (inti = 0; i < keys.count; i++) {
       
// 先把Key取出来
       
NSString *key = keys[i];
       
// 取出每个key对应的数组
       
NSArray *values =[plistDic objectForKey:key];
       
//我从下面的KVC跳上来的
       
// 创建数组 保存赋值完成的model
       
       
NSMutableArray *tempArray = [NSMutableArrayarray];
       
// 继续遍历每个数组
       
for (NSDictionary*dic in values) {
           
           
// 一个字典对应一个model
           
// 创建model
           
CellModel *model = [[CellModelalloc]init];
           
// model进行赋值
           
// 1. 一般赋值 一般不这么写 属性多了的话 太麻烦
           
//model.title = [dic objectForKey:@"title"];
           
//(KVC)赋值 一般都这么写
            [model
setValuesForKeysWithDictionary:dic];
           
// model 加到临时的可变数组里
            [tempArray
addObject:model];
            [model
release];
        }
        [
self.dataDicsetObject:tempArrayforKey:key];
    }
   
NSLog(@"%@",self.dataDic);
}


- (
void)addTableView
{
   
UITableView *tableView = [[UITableViewalloc]initWithFrame:[UIScreenmainScreen].boundsstyle:(UITableViewStyleGrouped)];
    tableView.
delegate= self;
    tableView.
dataSource= self;
    [
self.viewaddSubview:tableView];
    [tableView
release];
}

- (
NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
   
   
// 取出所有key
   
NSArray *keys = [self.dataDicallKeys];
   
NSString *key = keys[section];
   
NSArray *values = [self.dataDicobjectForKey:key];
   
return values.count;
}


- (
UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
   
NSString *identifier = @"JJcell";
   
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
   
if (cell == nil) {
         cell = [[
UITableViewCellalloc]initWithStyle:(UITableViewCellStyleSubtitle)reuseIdentifier:identifier];
    }
   
   
// 取出所有key
   
NSArray *keys = [self.dataDicallKeys];
   
NSString *key = keys[indexPath.section];
   
NSArray *values = [self.dataDicobjectForKey:key];
   
CellModel *model = values[indexPath.row];
    cell.
textLabel.text= model.title;
    cell.
imageView.image=[UIImageimageNamed:model.imageName];
   
return cell;

}
- (
NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
   
return self.dataDic.count;
}
- (
void)didReceiveMemoryWarning {
    [
superdidReceiveMemoryWarning];
   
// Dispose of any resources that can be recreated.
}
- (
void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
   
NSLog(@"%ld分区 %ld", indexPath.section, indexPath.row);
}

CellModel.m文件
- (void)dealloc
{
    [
_titlerelease];
    [
_imageNamerelease];
    [
superdealloc];
}
// KVC赋值保护 如果配对错了就会 抛出异常 可以直接打印出错的key 否则直接崩溃
对它进行空实现
- (void)setValue:(id)value forUndefinedKey:(NSString*)key
{
   
}














0 0
原创粉丝点击