Objective-c——UI基础开发第六天(UITableView)
一、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)的更多相关文章
- Objective-c——UI基础开发第七天(自定义UITableView)
一.梗概: 1.自定义:headerView,footerVie,Cell等 2.双模型(遵循单一原则,类或模型实现的功能尽量单一) 3.计算文本的方法(针对不同文本内容而设置的宽高等) 4.设置fo ...
- Objective-c——UI基础开发第十一天(UICollectionView)
一.知识点 1.UICollectionView的dataSource .delegate 2.UICollectionView多组数据和单组数据的展示 3.UICollectionView.UICo ...
- Objective-c——UI基础开发第九天(QQ好友列表)
一.知识点: 1.双模型的嵌套使用 2.Button的对齐方式 3.优化UITableView的加载 4.layoutSubview的使用 5.cell的折叠代理 二.双模型的嵌套定义: 注意是将se ...
- Objective-c——UI基础开发第八天(QQ聊天界面)
一.知识点: QQ聊天界面 双模型的使用(dataModel和frameModel) UITextField的使用 通知的使用 拉伸图片的两种方法(slicing/image对象的resizeable ...
- Objective-c——UI基础开发第十二天(相册展示)
一.知识点 模仿新特性 UICollectionViewFlowLayout自定义布局 相册 瀑布流(淘宝购物之类的 二.复习 a.UICollectionView 和 tableview共享一套AP ...
- Objective-c——UI基础开发第十天(自动布局)
一.autoresizing 的使用(了解) 只能参照父控件 1.实现横竖屏幕切换,不能把控件的frame血丝,需要进行屏幕适配 2.需要参照父控件 use auto layout禁用 才会出现aut ...
- iOS UI基础-9.1 UITableView 团购
概述 接下来,我们要做的是团购界面的设计,最张要实现的效果图及项目结构图 团购数据的展示 思路: 系统自带的tableCell不能展示三个文本,不能满足条件,自定义tableCell 每一个 ...
- iOS UI基础-9.2 UITableView 简单微博列表
概述 我们要实现的效果: 这个界面布局也是UITableView实现的,其中的内容就是UITableViewCell,只是这个UITableViewCell是用户自定义实现的.虽然系统自带的UITab ...
- iOS UI基础-9.0 UITableView基础
在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView.UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳. UITableView有两种样式: ...
随机推荐
- 五大要求让BPM与企业对接
BPM(即业务流程管理)在中国已经有多年的发展历史,但人们经常提到的还是企业对流程的迫切需要,鲜有人讨论什么样的企业才能实施BPM,或者换句话说BPM的本身对企业有什么要求.不是所有的工作都适合BPM ...
- hdu 1033 (bit masking, utilization of switch, '\0' as end of c string) 分类: hdoj 2015-06-15 21:47 37人阅读 评论(0) 收藏
bit masking is very common on the lower level code. #include <cstdio> #include <algorithm&g ...
- 更改layout的布局
创建了一个layout,缺省都是LinearLayout,原来都在xml把他改成其他布局,习惯了拖放的方式觉得挺麻烦. 其实vs2013有这个功能,就是在设计界面单机右键可以删除布局,然后拖一个布局进 ...
- STM32串口USART1的使用方法
前言: 通用同步异步收发器(USART)提供了一种灵活的方法来与使用工业标准NR 异步串行数据格式的外部设备之间进行全双工数据交换. USART利用分数波特率发生器提供宽范围的 波特率选择,支持同 ...
- jQuery easyui 提示框
1:弹出提示窗的使用 (1)屏幕右下弹出提示窗口: $.messager.show({ title:'My Title', msg:'Message will be closed after 4 se ...
- In p = new Fred(), does the Fred memory “leak” if the Fred constructor throws an exception?
No. If an exception occurs during the Fred constructor of p = new Fred(), the C++ language guarantee ...
- 超级链接a+ confirm用法
示例: <a href="DelServlet?action=${fuwa.id}" onClick="return confirm('你确定要删除?')" ...
- 自己搭建Wifi Pineapple Mark V
创业搞得自己很累,不过一切都是值得的.抽空写下文章,确实好久未更新了. 前段时间由于项目需要,所以就折腾了下wifi pineapple.时间间隔有点久,根据回忆记录下. 淘宝货:TP-Link TL ...
- php空心菱形
<?php for($i=1;$i<=9;$i++){ for($j=1;$j<=9-$i;$j++){ echo " "; } for($k=1;$k<= ...
- 深入分析:Android中app之间的交互(一,使用Action)
在我们开发Android App应用的时候,有些需求需要我们启动其他的App来处理一些逻辑,例如我们需要根据一个地址来调用系统或者相关的地图Map App,这样我们不用在自己的App中编写相应的功能, ...