- 浏览: 516351 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
tangyunliang:
大哥你太历害了谢谢
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
u013015029:
LZ,请问下,在// 添加消息到聊天窗口 , 这里获取Ed ...
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
endual:
怎么保持会话,我搞不懂啊
Android基于XMPP Smack Openfire开发IM【一】登录openfire服务器 -
donala_zq:
显示:[2013-11-30 11:50:36 - Andro ...
android-----------新浪微博 -
donala_zq:
哥,运行不了啊
android-----------新浪微博
我们常用的表格类视图就是用 UITableView与UITableViewCell,UITableViewController继承UIViewContoller,所以只要很少代码就可以显示一个视图,UITableViewController也是UIScrollView子类,所以也有上下滑动效果 ;UITableView和UITableViewCell不能储存数据,可以用来显示特定行数内的数据,而且,也并不是把所有数据都放在单元格cell视图上,而是通过单元格重用和实现UITableViewDataSource,UITableViewDelegate协议的方法形式显示出来;
1.新建工程名为SampleTable , File->New->Project ->single View Application -> next
[img]
[/img]
2.添加UITableViewDataSource,UITableViewDelegate协议
声明了一个存放数据的数组和用于显示单元格的两个对象
2.在@implementation STViewController后面添加上
@synthesize listData=_listData;
@synthesize tableView = _tableView;
@synthesize tableViewCell =_tableViewCell
viewDidLoad中实现对界面初始化工作,UITableView有两种风格,
UITableViewStylePlain 默认风格,最常见的
UITableViewStyleGrouped 圆角矩形风格
[img]
[/img]
[img]
[/img]
3.视图上显示单元格的内容以及一些数据都是都是属性都是依赖于协议的代理方法
UITableView每一行都有一个UITableViewCell的实例表示,它也继承UIView,也就是每一行又拥有一个子视图,如果是大型表格,这样开销就非常大,所以就有了单元格的重用;当一部分单元格滚出屏幕后,他们被放在一个可重用的单元序列之中。如果系统运行比较慢,表视图就会从序列中删除这些单元,释放空间,如果有储存空间,表视图就会重新获取这些单元,以后面使用;
注释内容中有这两个设置表视图背景颜色的属性方法,具体了解可以看这个博客http://haoxiang.org/2010/12/uitableviewcell-background/ 讲的比较详细
表示UITableViewCell风格的常量有:
UITableViewCellStyleDefault
UITableViewCellStyleSubtile
UITableViewCellStyleValue1
UITableViewCellStyleValue2
[img]
[/img]
[img]
[/img]
[img]
[/img]
[img]
[/img]
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
这个方法返回指定的 section的header view 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
这个方法返回指定的 section的footer view 的高度。
为了增加效果,所以界面显得比较丑陋,附上运行结果截图
[img]
[/img]
[img]
[/img]
1.新建工程名为SampleTable , File->New->Project ->single View Application -> next
[img]
[/img]
2.添加UITableViewDataSource,UITableViewDelegate协议
#import <UIKit/UIKit.h> @interface STViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property(strong,nonatomic) NSArray *listData; @property(strong,nonatomic)UITableView *tableView; @property(strong,nonatomic)UITableViewCell *tableViewCell; @end
声明了一个存放数据的数组和用于显示单元格的两个对象
2.在@implementation STViewController后面添加上
@synthesize listData=_listData;
@synthesize tableView = _tableView;
@synthesize tableViewCell =_tableViewCell
viewDidLoad中实现对界面初始化工作,UITableView有两种风格,
UITableViewStylePlain 默认风格,最常见的
UITableViewStyleGrouped 圆角矩形风格
[img]
[/img]
[img]
[/img]
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //初始化表格 self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; // 设置协议,意思就是UITableView类的方法交给了tabView这个对象,让完去完成表格的一些设置操作 self.tableView.delegate=self; self.tableView.dataSource=self; //把tabView添加到视图之上 [self.view addSubview:self.tableView]; // 存放显示在单元格上的数据 NSArray *array = [NSArray arrayWithObjects:@"张三",@"张四",@"张五",@"李三",@"李四",@"李五",@"李六",@"王三",@"王四",@"王五",@"王六",@"王七",@"王八",@"王九",@"王十", nil]; self.listData = array; }
3.视图上显示单元格的内容以及一些数据都是都是属性都是依赖于协议的代理方法
//返回多少个section -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
//返回行数,也就是返回数组中所存储数据,也就是section的元素 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.listData count]; }
UITableView每一行都有一个UITableViewCell的实例表示,它也继承UIView,也就是每一行又拥有一个子视图,如果是大型表格,这样开销就非常大,所以就有了单元格的重用;当一部分单元格滚出屏幕后,他们被放在一个可重用的单元序列之中。如果系统运行比较慢,表视图就会从序列中删除这些单元,释放空间,如果有储存空间,表视图就会重新获取这些单元,以后面使用;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 声明静态字符串型对象,用来标记重用单元格 static NSString *TableSampleIdentifier = @"TableSampleIdentifier"; // 用TableSampleIdentifier表示需要重用的单元 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier]; // 如果如果没有多余单元,则需要创建新的单元 if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier]; } else { while ([cell.contentView.subviews lastObject ]!=nil) { [(UIView*)[cell.contentView.subviews lastObject]removeFromSuperview]; } } // 获取当前行信息值 NSUInteger row = [indexPath row]; // 填充行的详细内容 cell.detailTextLabel.text = @"详细内容"; // 把数组中的值赋给单元格显示出来 cell.textLabel.text=[self.listData objectAtIndex:row]; // cell.textLabel.backgroundColor= [UIColor greenColor]; // 表视图单元提供的UILabel属性,设置字体大小 cell.textLabel.font = [UIFont boldSystemFontOfSize:40.0f]; // tableView.editing=YES; /* cell.textLabel.backgroundColor = [UIColor clearColor]; UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame]; backgroundView.backgroundColor = [UIColor greenColor]; cell.backgroundView=backgroundView; */ // 设置单元格UILabel属性背景颜色 cell.textLabel.backgroundColor=[UIColor clearColor]; // 正常情况下现实的图片 UIImage *image = [UIImage imageNamed:@"2.png"]; cell.imageView.image=image; // 被选中后高亮显示的照片 UIImage *highLightImage = [UIImage imageNamed:@"1.png"]; cell.imageView.highlightedImage = highLightImage; return cell; }
注释内容中有这两个设置表视图背景颜色的属性方法,具体了解可以看这个博客http://haoxiang.org/2010/12/uitableviewcell-background/ 讲的比较详细
cell.textLabel.backgroundColor= [UIColor greenColor];
cell.textLabel.backgroundColor = [UIColor clearColor]; UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame]; backgroundView.backgroundColor = [UIColor greenColor]; cell.backgroundView=backgroundView;
表示UITableViewCell风格的常量有:
UITableViewCellStyleDefault
UITableViewCellStyleSubtile
UITableViewCellStyleValue1
UITableViewCellStyleValue2
[img]
[/img]
[img]
[/img]
[img]
[/img]
[img]
[/img]
//设置单元格高度 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 90; }
//设置单元格缩进 -(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger row = [indexPath row]; if (row % 2==0) { return 0; } return 2; }
//选中单元格所产生事件 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 首先是用indexPath获取当前行的内容 NSInteger row = [indexPath row]; // 从数组中取出当前行内容 NSString *rowValue = [self.listData objectAtIndex:row]; NSString *message = [[NSString alloc]initWithFormat:@"You selected%@",rowValue]; // 弹出警告信息 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; }
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
这个方法返回指定的 section的header view 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
这个方法返回指定的 section的footer view 的高度。
为了增加效果,所以界面显得比较丑陋,附上运行结果截图
[img]
[/img]
[img]
[/img]
- SampleTable.zip (105 KB)
- 下载次数: 2
发表评论
-
新风作浪博客学习(十九)在iOS虚拟键盘上添加动态隐藏按钮
2013-06-08 09:19 814为了给用户比较良好的交付,想在键盘上添加一个按钮,实时根据键盘 ... -
新风作浪博客学习(十八)openURL的使用(iOS调用系统电话、浏览器、地图、邮件等) .
2013-06-08 09:19 931今天遇见一行代码实现打开一个网页,比起印象里的UIWebVie ... -
新风作浪博客学习(十七)UIImageView响应点击事件 .
2013-06-08 09:19 666有时候会遇到点击一张图片,然后让这张图片触发一个事件,或者是跳 ... -
新风作浪博客学习(十六)Navigation + Tab Bar 常用组合框架 .
2013-06-07 08:50 1193看到很多项目中都采用的是Navigation加Tab Bar组 ... -
新风作浪博客学习(十五)google地图定位小Demo .
2013-06-07 08:50 1057[img][/img]今天写的是一个简单功能的google地图 ... -
新风作浪博客学习(十四)怎样向iPhone模拟器中添加图片 .
2013-06-07 08:50 741在我们做项目中可能需要使用图库,模拟器是有图库的,但是如何像其 ... -
新风作浪博客学习(十三)表视图的分组分区和索引分区 .
2013-06-07 08:50 744本次实现的是表视图的分区和索引,代码和前面都差不多,主要还是代 ... -
新风作浪博客学习(十二)代码实现UITableViewCell表视图单元定制 .
2013-06-07 08:49 932通常情况下我们会希望单元格UITableViewCell显示自 ... -
新风作浪博客学习(十一)UITableViewCell的标记、移动、删除、插入 .
2013-06-06 09:15 1058这篇文章是建立在 代码实现 UITableView与UITa ... -
新风作浪博客学习(九)两个UIPickerView控件间的数据依赖 .
2013-06-06 09:14 1039本篇实现功能是两个选取器的关联操作,滚动第一个滚轮第二个滚 ... -
新风作浪博客学习(八)代码实现UIPickerView .
2013-06-06 09:14 1240先说一下当个组件选取器,我们创建一个数组NSAray来保存选取 ... -
新风作浪博客学习(七)代码 实现UIDatePicker控件 和 Tab Bar 视图切换 .
2013-06-06 09:15 1073感觉代码写控件都一个理,先在ViewDidLoad中创建控件对 ... -
新风作浪博客学习(六)ios 视图切换翻页效果 .
2013-06-05 11:18 1024本文写的是视图切换,涉及到的内容有 1.实现代码添加Navi ... -
新风作浪博客学习(五)代码实现UISlider 和 UISwitch .
2013-02-18 09:15 1074本次实现的UISlider和UISwi ... -
新风作浪博客学习(四)把plist里数据显示在textField上 .
2013-02-18 09:15 884在代码实现Lable 、textFie ... -
新风作浪博客学习(三)NSBundle读取图片 plist文件和txt文件
2013-02-18 09:15 1623本文想简单介绍一下NSBundle读取图片到视图上,读取pli ... -
新风作浪博客学习(二)代码实现Lable 、textField创建界面以及键盘的处理
2013-02-18 09:15 1137今天写的是用代码实现一个简单界面,代码重复率比较高,可读性不是 ... -
新风作浪博客学习(一)plist文件读写操作
2013-02-18 09:14 1299文件plist 全名Property List,属性列表文件, ... -
GCDiscreetNotificationView提示视图
2013-06-05 11:17 525先看一下效果图: [img] ... -
iphone开发之适配iphone5
2013-06-05 11:15 1010iphone5出来了,从不用适配的我们也要像android一样 ...
相关推荐
http://blog.csdn.net/duxinfeng2010/article/category/1155790 有详细介绍
UITableView用来以表格的形式显示数据。关于UITableView,我们应该注意: ...(4)所有的UITableView实现时实际上只有一列,但是我们可以通过向UITableViewCell中添加子视图,使得它看起来有好几列。
UITableView 详细讲解
代码仅供参考,有问题可以 联系我
史上最科学!Swift 3 UITableView最佳实践 XIB极速实现UITableViewCell,UITableViewHeaderFooterView,此处代码由Swift3.1展示,推荐使用Swift,项目已经上传至github中SimplifiedCellHeaderFooter...
通过自定义UITableViewCell实现ibooks的图书列表样式。
UITableView-Swift
纯代码创建的简单UITableView, 适合新手学习查看
主要介绍了IOS UITableView和UITableViewCell的几种样式详细介绍的相关资料,需要的朋友可以参考下
在iOS开发的过程中,UITableview是使用频率很高的控件之一,今天写的优化方法不是关于性能优化方面的,主要从为Controller瘦身方面考虑的。在使用tableView的时候不可避免的要谈到tableView的delegate和dataSource两...
一行代码搞定Uitableview 头部下拉放大
点击 TableView的某一行 实现TableViewcell 展开和收起。
这段代码来自我的视频教程,该视频教程介绍了如何在iOS 7中为UITableViewCell对象设置自动布局。 有一些陷阱(很多)可以使事情正常进行。 视频: : 内容摘自史蒂夫·乔布斯(Steve Jobs)的一些名言,它们可能...
列表的右侧字母索引和分组头部
简单的模拟QQ聊天界面
UITableView with stack animation..zip,带堆栈动画的uitableview。
Android应用源码之UITableView ios风格控件.rar
主要给大家介绍了关于iOS如何固定UITableView中cell.imageView.image图片大小的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来...