OC语言——————表视图
2016-07-12 00:00
393 查看
摘要: UITableView的一些方法,以及绘制cell,单元格复用的问题。
穿插相关的Xib
(新手路过,欢迎指教)
表视图UITableView
使用沙盒获取图片和plist文件的路径,调用类方法:(NSString *)[[NSBundle mainbundle] pathForResource:(NSString *)ofType:];
接下来就是表视图UITableView;
手写的时候,需要先实例化一个tableView,(UITableView *)[[UITableView alloc] initWithFrame:(CGRect) style:(EnUITableViewStyle)];
UITableViewSytlePlain(简约风格)和UITableViewStyleGrouped(分组风格);
加载到父视图[self.view addSubview:(UITableView *)];
需要遵守两个协议:UITableViewDataResource和UITableViewDelegate
实现UITableViewDataSource下面两个必须实现的方法:
//设置行数
-(NSInteger)tableView:(UITableview *)tableView numberOfRowsInSection:(NSInteger)section;
//绘制单元格(cell)
-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//单元格的复用,避免重复创建对象
static NSString *reuse=@“cell”;
//从队列里面实例化一个UITableViewCell的对象
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
if(cell==nil)
{
//实例化的时候,单元格的显示风格有四种:1.UITableViewCellStyleDefault 默认风格,仅显示一行文字
2.UITableViewCellStyleValue1 存在imageView,textLabel,detailTextLabel,细节描述在最右边
3.UITableViewValue2 不存在imageView
4.UITableViewSubtitle 存在imageView,textLabel,detailTextLabel,细节描述位于textLabel 的下方
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
}
cell.textLabel.text //设置单元格显示的内容
cell.imageView.image //设置单元格里面显示的图片内容
cell.detailTextlabel.text //设置细节描述
return cell;
}
需要设置代理和数据源【delegate,datasource】的对象,才能触发协议里面的方法;
还可以设置分组的组数:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
设置行高:
-(CGFloat )tableView :( UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 行高;
}
处理cell里面自带imageView的大小的方法:
cell.imageView.image = [UIImage imageNamed:@"光影.jpg"];
CGSize itemSize = CGSizeMake(80, 80);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
后面学习的UITableViewController,需要直接添加一个UITableViewController,然后新建一个UITableView的类,在新建的类的.m文件里面设置行数,设置组,绘制单元格,需要更改Controller的初始状态。
穿插相关的Xib
(新手路过,欢迎指教)
表视图UITableView
使用沙盒获取图片和plist文件的路径,调用类方法:(NSString *)[[NSBundle mainbundle] pathForResource:(NSString *)ofType:];
接下来就是表视图UITableView;
手写的时候,需要先实例化一个tableView,(UITableView *)[[UITableView alloc] initWithFrame:(CGRect) style:(EnUITableViewStyle)];
UITableViewSytlePlain(简约风格)和UITableViewStyleGrouped(分组风格);
加载到父视图[self.view addSubview:(UITableView *)];
需要遵守两个协议:UITableViewDataResource和UITableViewDelegate
实现UITableViewDataSource下面两个必须实现的方法:
//设置行数
-(NSInteger)tableView:(UITableview *)tableView numberOfRowsInSection:(NSInteger)section;
//绘制单元格(cell)
-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//单元格的复用,避免重复创建对象
static NSString *reuse=@“cell”;
//从队列里面实例化一个UITableViewCell的对象
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
if(cell==nil)
{
//实例化的时候,单元格的显示风格有四种:1.UITableViewCellStyleDefault 默认风格,仅显示一行文字
2.UITableViewCellStyleValue1 存在imageView,textLabel,detailTextLabel,细节描述在最右边
3.UITableViewValue2 不存在imageView
4.UITableViewSubtitle 存在imageView,textLabel,detailTextLabel,细节描述位于textLabel 的下方
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
}
cell.textLabel.text //设置单元格显示的内容
cell.imageView.image //设置单元格里面显示的图片内容
cell.detailTextlabel.text //设置细节描述
return cell;
}
需要设置代理和数据源【delegate,datasource】的对象,才能触发协议里面的方法;
还可以设置分组的组数:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
设置行高:
-(CGFloat )tableView :( UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 行高;
}
处理cell里面自带imageView的大小的方法:
cell.imageView.image = [UIImage imageNamed:@"光影.jpg"];
CGSize itemSize = CGSizeMake(80, 80);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
后面学习的UITableViewController,需要直接添加一个UITableViewController,然后新建一个UITableView的类,在新建的类的.m文件里面设置行数,设置组,绘制单元格,需要更改Controller的初始状态。
相关文章推荐
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- ios8 UITableView设置 setSeparatorInset UIEdgeInsetsZero不起作用的解决办法(去掉15px空白间距)
- iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码
- iOS程序开发中设置UITableView的全屏分隔线的方法(不画线)
- 讲解iOS开发中UITableView列表设计的基本要点
- iOS应用开发中UITableView的分割线的一些设置技巧
- 实例讲解iOS应用开发中使用UITableView创建自定义表格
- iOS App开发中使用及自定义UITableViewCell的教程
- 全面解析iOS应用中自定义UITableViewCell的方法
- iOS App中UITableView左滑出现删除按钮及其cell的重用
- 详解iOS开发中UITableview cell 顶部空白的多种设置方法
- iOS开发中UITableview控件的基本使用及性能优化方法
- 详解iOS开发中UItableview控件的数据刷新功能的实现
- UITableView 实现汽车品牌(demo)
- 改变iOS应用中UITableView的背景颜色与背景图片的方法
- iOS开发之UITableView详解
- 详解iOS App中UITableView的创建与内容刷新
- IOS开发笔记(Swift):UITableView表格视图的静态使用
- IOS 9 UITableView整理
- UITableView 基本使用方法总结