UITaleView的基础使用及数据展示操作
UITableView表视图,是实用的数据展示的基础控件,是继承于UIScrollView,所以也可以滚动。但不同于UIScrollView,UITableView只可以上下滚动,而不能左右滚动。
因为是数据展示,必然少不了数据的存在,嗯,使用plist文件来获取想要的数据。通过模型来获取。
说到这样就必须要变到MVC了,MVC是设计模块的核心
M是指模型,用来获取数据
V是指视图,用来展示各种数据,各种控件
C是指视图控制器,能来维持M和V之间的通信
创建一个类,将相要展示的数据声明成属性
@interface Student : NSObject @property (nonatomic, copy)NSString *name;
@property (nonatomic, copy)NSString *age;
@property (nonatomic, copy)NSString *number;
@property (nonatomic, copy)NSString *gender;
@property (nonatomic, copy)NSString *hobby;
一定要记得重写处理异常的方法,因为有可能有些数据没有使用到,或者数据拼写错误都不会 产生异常处理。
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{ }
因为要使用模型,所以 用类似于懒加载的方式,来加载数据
因为 是在MRC环境下,所以操作完之后要记得释放
在延展中创建一个保存数据的可变数组dataArrary;
@implementation ListViewController
- (void)dealloc{
[_dataArrary release];
[_tableView release];
[super dealloc];
}
//加载数据的方法
- (void)reloadData{
//获取文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
//从文件路径中提取数组
NSArray *arrary = [NSArray arrayWithContentsOfFile:filePath];
//初始化数组
_dataArrary = [[NSMutableArray alloc] initWithCapacity:];
//遍历数组,进行添加模型
for (NSDictionary *dic in arrary) {
Student *student = [[Student alloc] init];
[student setValuesForKeysWithDictionary:dic];
[_dataArrary addObject:student];
[student release]; //一定要释放!
}
}
创建UITableView.
//执行加载的方法
[self reloadData]; _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain]; self.navigationItem.leftBarButtonItem = self.editButtonItem; //代理
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
因为要展示数据,所以必须要使用系统内部提供的两个协议,数据源协议UITableDataSource,代理协议UITableDelegate
因为签订了UITableDataSource协议,所以有两个必须要实现的方法
//每个分区多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//每行显示的内容,也就cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
#pragma mark ------数据源协议的方法--------
//返回分区多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_dataArrary count];
}
//每行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//标识符
static NSString *reuseIdentifier = @"reuse";
//通过重用标识符找cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//判断是否为空,是的话重新创建
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]autorelease];
}
//选中的一行不会有格式显示,默认选中的为灰色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [[_dataArrary objectAtIndex:indexPath.row] name];
cell.detailTextLabel.text = [[_dataArrary objectAtIndex:indexPath.row] age];
return cell;
}
因为 要注意重用池的使用,如果 cell每次使用都要重新创建的话,那会造成很大的内存负担,如果使用重用池,当cell滑出屏幕之外,被滑出的cell会放入重用池中,当下次使用,根据重用标识符来判断,如果为空,则重新创建,否则则使用已有的cell。
每个cell都相当于button的作用,都可以点击,都可以选定,所以系统内部也规定了,cell有专门的点击方法
//点击单元格触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//push操作,传值
DetailViewController *detailVC = [[DetailViewController alloc] init];
detailVC.student = _dataArrary[indexPath.row];
[self.navigationController pushViewController:detailVC animated:YES];
[detailVC release];
}
对表视图也可以 进行一些操作,比如删除,添加和移动。
如果想删除和添加的话有四步可以 实现 这两个功能
//增加删除功能显示 并不能执行删除操作
[_tableView setEditing:YES animated:YES];
每一个视图控制器都有一个编辑按钮,因为项目中编辑的应用场景非常多,所以系统预留了一个编辑按钮供我们使用。
#pragma mark -------删除,添加数据---------- //1.让将要执行删除,添加操作的表视图处于编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
//先执行父类中的这个方法
[super setEditing:editing animated:animated];
//表视图执行此方法
[self.tableView setEditing:editing animated:animated];
} //2.指定表视图中哪些行可以处于编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
//前五行可以处于编辑状态
// if (indexPath.row < 5) {
// return YES;
// }
// return NO;
//这个方法默认的是全部行都可以进行编辑
return YES;
} //3.指定编辑样式,到底是删除还是添加
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//这个方法默认的是删除样式,即UITableViewCellEditingStyleDelete //添加样式
//return UITableViewCellEditingStyleInsert; //前六行是删除样式,之后的全部行是添加样式
// if (indexPath.row < 6) {
// return UITableViewCellEditingStyleDelete;
// }else{
// return UITableViewCellEditingStyleInsert;
// }
}
//4.不管是删除还是添加,这个方法才是操作的核心方法,当点击删除或者添加按钮时,需要做什么事情,怎么样才能完成删除还是添加操作,全部在这个方法内部指定。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//表视图开始更新
[tableView beginUpdates]; if (editingStyle == UITableViewCellEditingStyleDelete) {
//将该位置下的单元格删除
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//删除数据数组中,与该单元格绑定的数据‘
[_dataArrary removeObjectAtIndex:indexPath.row];
}else if (editingStyle == UITableViewCellEditingStyleInsert){
Student *student = _dataArrary[indexPath.row];
//构建一个位置信息
NSIndexPath *index = [NSIndexPath indexPathForRow: inSection:];
[tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];
[_dataArrary insertObject:student atIndex:index.row];
}
//表视图结束更新
[tableView endUpdates];
}
同样的 ,移动也是相同的步骤,不同于删除和添加的方法就是核心处理方式的不同,也就是第四步
#pragma mark ------表视图移动的操作---------
//1.移动的第一步也是需要将表视图的编辑状态打开
//2.指定哪些行可以进行移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
//默认都可以移动
return YES;
} //3.移动完成之后要做什么事,怎么完成移动
//sourceIndexPath原位置
//destinationIndexPath新位置
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//先记录原有位置下的模型数据
Student *student = _dataArrary[sourceIndexPath.row];
[student retain];
//删除原位置下的模型数据
[_dataArrary removeObjectAtIndex:sourceIndexPath.row];
//在新位置将记录的模型数据添加到数据数组中
[_dataArrary insertObject:student atIndex:destinationIndexPath.row];
[student release];
}
移动呢,就是先记录好原有位置的数据,再删除原位置的数据,然后在新位置上,把记录的数据添加到模型数组中。
注意:
UITableView,是展示数据的基础,对于新手来说,还是很容易上手的,数据源协议有除了两个必要的方法之外,还可以通过方法来显示有多少个分区,并显示分区标题,根据分区显示索引。
UITaleView的基础使用及数据展示操作的更多相关文章
- layui数据表格使用(一:基础篇,数据展示、分页组件、表格内嵌表单和图片)
表格展示神器之一:layui表格 前言:在写后台管理系统中使用最多的就是表格数据展示了,使用表格组件能提高大量的开发效率,目前主流的数据表格组件有bootstrap table.layui table ...
- ExtJS4.2学习(21)动态菜单与表格数据展示操作总结篇2
运行效果: 此文介绍了根据操作左侧菜单在右面板展示相应内容. 一.主页 先看一下跳转主页的方式:由在webapp根目录下的index.jsp跳转至demo的index.jsp 下面是demo的inde ...
- EasyUI-datagrid数据展示+MongoDB数据操作
使用EasyUI-datagrid进行数据展示:进行添加,修改,删除操作逻辑代码,数据源来自MongoDB. 一.新建SiteInfo控制器,添加Index页面:http://www.cnblogs. ...
- AI学习---数据IO操作&神经网络基础
数据IO操作 TF支持3种文件读取: 1.直接把数据保存到变量中 2.占位符配合feed_dict使用 3. QueueRunner(TF中特有的) 文件读取流程 文件读取流程(多线 ...
- 使用HighCharts实现实时数据展示
在众多的工业控制系统领域常常会实时采集现场的温度.压力.扭矩等数据,这些数据对于监控人员进行现场态势感知.进行未来趋势预测具有重大指导价值.工程控制人员如果只是阅读海量的数据报表,对于现场整个态势的掌 ...
- java基础-多线程应用案例展示
java基础-多线程应用案例展示 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.两只熊,100只蜜蜂,蜜蜂每次生产的蜂蜜量是1,罐子的容量是30,熊在罐子的蜂蜜量达到20的时候 ...
- WebGIS中利用AGS JS+eChart实现一些数据展示的探索
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 eChart提供了迁徙图.热点图.夜视图等跟地图能够很好的结 ...
- JAVASE02-Unit07: 基本IO操作 、 文本数据IO操作
基本IO操作 . 文本数据IO操作 java标准IO(input/output)操作 package day07; import java.io.FileOutputStream; import ja ...
- 【助教】Java获取数据库数据展示
本文将给出一个最简单的Java查询数据库中一张表的数据并将查询结果展示在页面的例子. 实际上,我们要解决以下两个问题: Java与数据库交互(以JDBC为例) 数据展示在前台页面(以Servlet+J ...
随机推荐
- PTA作业
- 三部曲二(基本算法、动态规划、搜索)-1003-Lucky and Good Months by Gregorian Calendar
模拟加阅读题......虽然很多事常识性的知识,但也有许多不知道的知识,关键是不读不知道那些是已经知道的那些不是,许多重要的信息零散的分布在一大坨英文里,读起来很痛苦......自己读了一遍,读的晕晕 ...
- C#数据结构
数据结构是相互之间存在一种或多种特定关系的数据元素的集合.在任何问题中,数据元素之间都不是孤立的,而是存在着一定的关系,这种关系称为结构(Structure).根据数据元素之间关系的不同特性,通常有 ...
- php之无限极分类
首先建立分类信息表: CREATE TABLE IF NOT EXISTS `category` ( `categoryId` smallint(5) unsigned NOT NULL AUTO_I ...
- Ingress qdisc
Ingress qdisc All qdiscs discussed so far are egress qdiscs. Each interface however can also have an ...
- Linux TC流量控制HOWTO中文版
<本文摘自Linux的高级路由和流量控制HOWTO中文版 第9章节>网人郭工进行再次编译: 利用队列,我们可以控制数据发送的方式.记住我们只能对发送数据进行控制(或称为整形).其实,我们无 ...
- HTML、canvas、video灰度
效果图: 注:本例需在服务器上运行的才能看到效果.视频文件可换成本地视频(HBuilder有集成服务器或者使用wampmanager). 代码如下: <!DOCTYPE html> < ...
- kernel/Makefile
## Makefile for the linux kernel.## Note! Dependencies are done automagically by 'make dep', which a ...
- SQLite手工注入方法小结
SQLite 官网下载:www.sqlite.org/download.html sqlite管理工具:http://www.yunqa.de/delphi/products/sqlitespy/in ...
- jQuery-2.1.4.min.js:4 Uncaught TypeError: Illegal invocation
jQuery-2.1.4.min.js:4 Uncaught TypeError: Illegal invocation 此错误与crsf有关