一、UITableView的简单使用

显示要素:

  1、显示多少给区组

  2、显示多少行数据

  3、每行显示什么内容

代理不会提醒你有什么方法没调用,但是UITableViewDataSource会

1)用代码创建一个UITableView

UITableView *tableview =[[UITableView alloc]initWithFrame:CGRectMakr(0,0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];

/**

注意 style:

UITableViewStylePlain

UITableViewStyleGrouped

区别:plain 的footer 和 header 具有浮动效果,并且中间没有间隔

grouped:footer和header之间具有一定的间距

*/

2)设置tableview datasource的代理方法

tableview.datasource =self;

3)调用三个一定要实现的代理方法

  3.1) numberOfSectionsInTableView 返回(NSInteger)

  3.2) numberOfRowsInSection 返回(NSInteger)

  3.3)cellForRowsAtIndex 返回(UITableViewCell*)

4)注意 三个代理方法的调用方式(当你设置3个section 分别显示1,2,2行row 的时候)

可以观测到:

调用section组数次numberOfSectionsInTableView方法

每次都会确认全部组对应的相应行数 调用numberOfRowsInSection

sections*rows

调用sections*rows次cellForRowsAtIndex方法

把对应每组每行都取过去

5)总结调用代理方法的过程和次数:

每次先调用获取总共的组数,然后传入组数,获取第一组的行数,再传入组数,获取第二组的行数,以此类推,最后,根据每组对应的每行 indexPath 属性进行内容传递

6)设置tableview section的header和footer

titleForFooterInSection/titleForHeaderInsection 返回(NSString*)

注意也可以用viewForFooterInSection/viewForHeaderInSection 不同的是,需要定义(UITableViewDelegate)这个代理,返回(UIView*)

直接返回就可显示

二、汽车展示:UITableView的数据绑定

1)创建model类

@property(nonatomic,copy)NSString *name;

@property(nonatomic,copy)NSString *icon;

-(instancetype)initWithDictionary:(NSDictionary*)dict;

+(instancetype) CarinitWithDictioary:(NSDictionary*)dict;

2)定义执行model类方法

-(instancetype) initWithDictionary:(NSDictionary*)dict

{//注意,之前错写成 Carmodel .init   报错不能初始化一个类NSObject

if(self =[super init])

{ [self setValuesForKeyWithDictionary:dict];}}

+(instancetype)CarinitWithDictionary:(NSDictionary*)dict{[return [self alloc]initWithDictionary:dict];}

3)进行懒加载,懒加载实际上就是重定义它的getter方法,而且可以在使用的时候分配内存,不使用的时候自动回收内存,节省内存消耗,并且可以避免重复实例化,各实例之间独立,同时也能够减少直接在viewDidLoad中定义参数的代码量

@property (nonatomic,strong) NSArray *dataArray;(如果将来会修改dataArray的值时,需要用NSMutableArray 才可以调用相应的赋值方法 addObjects..)

-(NSArray *) dataArray

{

NSString *path=[NSBundel mainBundel] pathForResourct :@"car.plist",ofType:nil];

NSArray *tempArray =[NSArray arrayWithContentsOfFile :path];

NSMutableArray *muArray =[NSMutableArray array];

for(NSDictionary *dict in tempArray)

{

CarModel *model =[CarModel CarinitWithDictionary:dict];

[muArray addObject:model];

}

_dataArray =muArray;

return _dataArray;

}

注意,

如果tableview中没有显示的时候,可以考虑:

  1、是否添加代理 tableview.delegate=self;

  2、考虑是否取出数据:path路径是否正确,懒加载是否成功?

三、一些其他的tableview 和tableviewcell 的属性

1)/*

cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; //箭头

cell.accessoryType=UITableViewCellAccessoryNone; //没有箭头

cell.accessoryType =UITableViewCellAccessoryCheckmark; //打勾

cell.accessoryType=UITableViewCellAccessoryDetailButton;//圆圈+i

*/

/*

cell.selectionStyle=UITableViewCellSelectionStyleBlue;//ios7以上默认为灰色

cell.selectionStyle=UITableViewCellSelectionStyleGray;

cell.selectionStyle=UITableViewCellSelectionStyleNone;//什么都不显示

cell.selectionStyle=UITableViewCellSelectionStyleDefault;//默认为灰色

cell.selectedBackgroundView设置背景view 但是不能改变宽高

*/

/**

tableview 的常见属性

1、rowHeight 行高

2、separatorColor 分割线颜色

3、separatorStyle 分割线样式

4、allowMultipleSelection 是否允许多行选择

*/

/**

cell的常见属性

1、selectionStyle

2、selectedBackgroundView 注意 view宽高是可以改变,但是相对位置无法改变

3、background 注意其backgroundview不设置frame 默认就是完整的一行宽高,即便改变,宽高的设置仍然是无效的

*/

/**

cell的内置控件

1、textLabel

2、detailLabel

3、imageview

4、accessoryType

5、accessoryView

*/

/**

UITableViewCellStyle

UITableViewCellStyleDefault

UITableViewCellStyleValue1

UITableViewCellStyleValue2

UITableViewCellStyleSubtitle

*/

/**

accessoryType

UITableViewCellAccessoryDetailDisclosureIndicator

UITableViewCellAccessorycheckwork

*/

/**

tableview.separatorStyle

UITableViewCellSeparatorStyleNone

UITableViewCellSepatratorStyleSingleLine

UITableViewCellSeparatorStyleSingleLineEtched

*/

/**

设置cell的选择样式

UITableViewCellSelectionsStyleNone

UITableViewCellSelectionsStyleGray

UITableViewCellSelectionsStyleDefault

UITableViewCellSelectionsStyleBlue

*/

2)注意为什么在tableview的section中加入了footer/header但是没有显示

如果直接控件拖拽UITableView 的话会产生一个默认的section高度

但是如果是代码添加的话,就需要手动设置:
tableview.sectionFooterHeight=20;

tableview.SectionHeaderHeight =20;

自定义accessoryview的时候,frame中的坐标值不能改变,只能改变其宽高

类似switch 的宽textfield的高 是不能修改的

3)设置tableview的行高有两种方式

  a、tableview.rowHeight =5;

  b  heightForRowAtIndexPath(UITableViewDelegate) 返回NSFloat

  唯一的区别是,用执行方法,可以获取indexpath 可以针对不同的行设置会变动的行高

四、cell的重用

1)了解为什么要重用cell

cell的浏览机制是,当用户滑动时,划出窗口的cell就会被废弃,然后进入的是一个重新创建好的cell,这样的话,一旦运行时间过长,不断的废弃重创, 会造成程序内存吃紧,效率非常低 隐患很大。每个cell小时之后,都会重新创建cell

cell重用机制:把移出的cell 放入缓存池,重复使用,而只是更换上面的数据

那么,当有多种不同类型的cell在tableview上显示的时候,怎么在缓存池中找到

重用标识符!即需要重用的cell起名 identifier

2)cell重用的操作过程

  1、定义重用标识符

  2、在缓存池中寻找

  3、判断缓存池中是否有相应的cell 如果没有的话,就重新创建

  4、赋值新数据

  a \

NSString *identifier =@"cell";

  b\

UITableViewCell *cell =[tableview dequeueReusableCellWithIdentifier:identifier];

  c\

if(cell ==nil){

    cell =[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

  d\

cell.textLabel.text =model.name;

}

五、模型的嵌套

1)在carmodel中导入 innermodel

if(self =[super init])

{

[self setValuesForKeyWithDictionary:dict];

ning 一直反应interface中没定义InnerCarinitWithDictionary方法 导入innercarmodel

// InnerCarModel *innerModel =[[InnerCarModel alloc] InnerCarinitWithDictionary写法是错误的导致,不需要alloc

NSMutableArray *muArray =[NSMutableArray array];

for(NSDictionary *dict in self.cars)

{

InnerModel *model =[InnerModel InnerinitWithDictionary];

[muArray addObject:model];

}

self.cars =muArray;

return self;

}

2)在tableview中显示

组数:self.dataArray.count

行数:CarModel *model =self.dataArray[section];

return model.cars.count;

内容:

CarModel *model =self.dataArray[indexPath.section];

InnerModel *innermodel =model.cars[indexPath.row];

六、cell的编辑,增删

Objective-c——UI基础开发第六天(UITableView)的更多相关文章

  1. Objective-c——UI基础开发第七天(自定义UITableView)

    一.梗概: 1.自定义:headerView,footerVie,Cell等 2.双模型(遵循单一原则,类或模型实现的功能尽量单一) 3.计算文本的方法(针对不同文本内容而设置的宽高等) 4.设置fo ...

  2. Objective-c——UI基础开发第十一天(UICollectionView)

    一.知识点 1.UICollectionView的dataSource .delegate 2.UICollectionView多组数据和单组数据的展示 3.UICollectionView.UICo ...

  3. Objective-c——UI基础开发第九天(QQ好友列表)

    一.知识点: 1.双模型的嵌套使用 2.Button的对齐方式 3.优化UITableView的加载 4.layoutSubview的使用 5.cell的折叠代理 二.双模型的嵌套定义: 注意是将se ...

  4. Objective-c——UI基础开发第八天(QQ聊天界面)

    一.知识点: QQ聊天界面 双模型的使用(dataModel和frameModel) UITextField的使用 通知的使用 拉伸图片的两种方法(slicing/image对象的resizeable ...

  5. Objective-c——UI基础开发第十二天(相册展示)

    一.知识点 模仿新特性 UICollectionViewFlowLayout自定义布局 相册 瀑布流(淘宝购物之类的 二.复习 a.UICollectionView 和 tableview共享一套AP ...

  6. Objective-c——UI基础开发第十天(自动布局)

    一.autoresizing 的使用(了解) 只能参照父控件 1.实现横竖屏幕切换,不能把控件的frame血丝,需要进行屏幕适配 2.需要参照父控件 use auto layout禁用 才会出现aut ...

  7. iOS UI基础-9.1 UITableView 团购

    概述 接下来,我们要做的是团购界面的设计,最张要实现的效果图及项目结构图      团购数据的展示 思路: 系统自带的tableCell不能展示三个文本,不能满足条件,自定义tableCell 每一个 ...

  8. iOS UI基础-9.2 UITableView 简单微博列表

    概述 我们要实现的效果: 这个界面布局也是UITableView实现的,其中的内容就是UITableViewCell,只是这个UITableViewCell是用户自定义实现的.虽然系统自带的UITab ...

  9. iOS UI基础-9.0 UITableView基础

    在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView.UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳. UITableView有两种样式: ...

随机推荐

  1. lastPathComponent的功能

    下面是官方的说明: 源字符串   --->     结果字符串 “/tmp/scratch.tiff”   --->     “scratch.tiff” “/tmp/scratch”   ...

  2. 开源留言板 --wekan部署

    1. 安装ubuntu--server-64位系统 2. 登录ubuntu系统 3. 下载自动安装脚本 #git clone https://github.com/anselal/wekan 4. 执 ...

  3. 2013年7月份第1周51Aspx源码发布详情

    启睿网络信息服务器实例源码  2013-7-5 [ VS2005 ]功能介绍:睿网络信息服务器,QiRui Net Information Services简称QRNIS,该软件前身系KCIS.当前版 ...

  4. poj1458

    //Accepted 4112 KB 16 ms //最长公共子串 #include <cstdio> #include <cstring> #include <iost ...

  5. C#中的五个访问修饰符

    一.public, private, protected, internal, protected internal 1.public : 公开的, 公共的 2.private : 私有的 (只能在当 ...

  6. c++高质量编程手册

    怡化主管强烈要求我读这本书.... 笔记尚未完成,持续更新呗.. 第1章 高质量软件开发之道 1.1 软件质量基本概念 1.1.1 如何理解软件的质量:功能性和非公能性 1.1.2 提高软件质量的基本 ...

  7. FineUI疑难杂症

    1. 导出excel操作:因为fineui里的按钮自带异步功能,所以要把 EnableAjax="false" DisableControlBeforePostBack=" ...

  8. 极客DIY:RFID飞贼打造一款远距离渗透利器

    本文使用最新的渗透工具RFID飞贼(Tastic RFID Thief)和RFID感应破解技术来获取一些拥有安防的建筑物的访问权限. Tastic RFID Thief是一个无声远距离RFID读卡器, ...

  9. Android的切图标准

    最近总是有人在问我,Android怎么切图啊,怎么适配啊,不只是Android同行,还有很多新手ui设计师. 于是我就写篇文章,根据我们平时的开发经验,简单的介绍一下吧. 如果UI设计师以1920*1 ...

  10. 移动端 meta 标签笔记

    (转自http://www.cssue.com/xhtml-css/html5-css3/mobile-meta.html,版权归原作者所有!) 移动平台对 meta 标签的定义 一.meta 标签分 ...