IOS开发-TableView表视图基础
来源:互联网 发布:eclipse 运行java程序 编辑:程序博客网 时间:2024/06/05 03:29
表视图在IOS中的应用非常广泛,常用于展示显示数据列表。
在工具组中包含了Table View 和Table View Cell
每个表示图都是UITableView的一个实例,每个可见行都是UITableViewCelll的一个实例。也就是把TableViewCell放入TableView里面。
表格有两种style
分组和列表
分组:Grouped
列表:Plain
下面做一个默认style的表视图实例:
1.先拉出一个TableView到xib中。
2.连接添加的TableView和ViewController
因为IOS开发是使用MVC模式,所以这里要把视图和控制层关联起来,而他们之间的桥梁就是File's Owner.这样一来控制器类就成了TableView的数据源和委托
3.向ViewController.h中添加
@property (nonatomic,retain) NSArray *list;
并使用
<UITableViewDelegate,UITableViewDataSource>协议
向ViewController.m添加实现
@synthesize list=_list;
4.建立数据
在ViewController.m中添加
- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. NSArray *array = [[NSArray alloc] initWithObjects:@"美国", @"菲律宾", @"黄岩岛", @"中国", @"泰国", @"越南", @"老挝", @"日本" , nil]; self.list = array; }- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view. self.list = nil;}
并实现协议的三个方法,这三个方法必须实现不然后果很严重!!!
//绑定数据源- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *TableSampleIdentifier = @"TableSampleIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier]; } NSUInteger row = [indexPath row]; cell.textLabel.text = [self.list objectAtIndex:row]; return cell;}//是否分组-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}//行数-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.list.count;}
运行结果:
这样一个简单的tableView例子就完成了 NICE!
5.为每一行加图片,首先添加图片文件到到项目中
修改方法:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
UIImage *img= [UIImage imageNamed:@"a.png"]; cell.imageView.image=img;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *TableSampleIdentifier = @"TableSampleIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier]; } UIImage *img= [UIImage imageNamed:@"a.png"]; cell.imageView.image=img; NSUInteger row = [indexPath row]; cell.textLabel.text = [self.list objectAtIndex:row]; return cell;}
效果如下:
6.设置表格的样式,说明文字
initWithStyle:UITableViewCellStyleSubtitle
cell.detailTextLabel.text =@"nice";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *TableSampleIdentifier = @"TableSampleIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:TableSampleIdentifier]; } UIImage *img= [UIImage imageNamed:@"a.png"]; cell.imageView.image=img; NSUInteger row = [indexPath row]; cell.textLabel.text = [self.list objectAtIndex:row]; cell.detailTextLabel.text =@"nice"; return cell;}
效果如下:
7.设置缩进级别
//缩进级别-(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row]; return row;}
效果如下:
那么一行会按照它的索引去增加缩进,nice~
8.处理行的选择
可以使用-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath这个方法来做处理
//行的选择-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *rowString = [self.list objectAtIndex:[indexPath row]]; UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alter show];}
效果如下:
9.设置字体的大小和行高
cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *TableSampleIdentifier = @"TableSampleIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:TableSampleIdentifier]; } UIImage *img= [UIImage imageNamed:@"a.png"]; cell.imageView.image=img; NSUInteger row = [indexPath row]; cell.textLabel.text = [self.list objectAtIndex:row]; cell.textLabel.font = [UIFont boldSystemFontOfSize:50]; cell.detailTextLabel.text =@"nice"; return cell;}
效果如下:
但这样我们又发现行高不对,所以我们需要在控制层类里面再添加一个委托方法来解决这个问题,
//委托解决行高问题-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 70;}
效果如下:
那么今天的TableView表视图基础知识就学习到这里。
以下是IOS开发-我的第一个IOS程序 及 IOS开发-TableView表视图基础 源码:
源码下载
- IOS开发-TableView表视图基础
- IOS开发-TableView表视图LV2
- iOS开发- tableView表视图设置背景图片
- iOS:tableView表视图顶部多余部分
- iOS tableView表视图设置背景图片
- iOS tableView表视图设置背景图片
- ios开发基础视图 - UIPickerView
- iOS开发学习笔记 (一)表视图基础 Table Views
- iOS开发仿【喵播】之快速集成tableView头部缩放视图+视图为模糊效果
- ios开发基础--滚动视图(UIScrollView)
- iOS开发之基础视图— UISwitch
- 【iOS开发】---- tableView背景随表滚动
- ios开发 tableview小结
- iOS开发 精简TableView
- 窗口,视图,视图控制器和UIKit基础-IOS开发
- 【iOS】iOS开发TableView分组
- iOS开发---当tableview滚到视图底部展开列表后看不到数据
- iOS视图学习——TableView
- Oracle RAC系列之:ASM基本操作维护
- 布局资源(layout)的简单使用
- 杭电2629
- java之线程学习心得一
- 有关汇丰软件面试帖
- IOS开发-TableView表视图基础
- Unknown character set: 'utf8mb4'
- I/O重定向 详解及例子!
- chrome拓展插件下载CRX文件|查看源代码
- 五分钟理解元类(Metaclasses)
- poj 3461 kmp
- 100 bugs in Open Source C/C++ projects
- Java代码签名与认证
- windows命令行设置网卡ip地址