ios开发入门篇(三):UITableView简介
最近做项目又开始用到了uitableview,温习之余,在这里把uitableview的用法分享一下,有不对的地方欢迎大家提出来。
废话不多说,先创建一个工程,由于Xcode6,去除了创建工程时的空项目的选项,我们继续选择single view application 在这里我们用不到main storyboard 先删掉,创建一个类,继承自
UINavigationController ,这里文件名字叫做HealthViewcont
然后在appdelegate里的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
添加如下代码:
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.window makeKeyAndVisible]; self.window.rootViewController = [[HealthViewcont alloc]init];
准备工作就完成了
-:UITableView的初始化
1.在.h文件里实现 UITableViewDataSource,UITableViewDelegate两个代理协议,如果你这里继承的时UITableView 可以不用写
然后定义两个对象
@property(nonatomic)UITableView* tableview;
@property(nonatomic)NSMutableArray* dataArryList;
在.m文件里实现
@synthesize tableview;
@synthesize dataArryList;
2.在viewdidload里添加如下代码
- (void)viewDidLoad {
[super viewDidLoad];
//初始化一个tableview
tableview = [[UITableView alloc]initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-)];
[self.view addSubview:tableview];
[self.navigationBar setBackgroundColor:[UIColor redColor]];
//实现代理
tableview.delegate = self;
tableview.dataSource = self;
//初始化数据
dataArryList = [[NSMutableArray alloc]initWithArray:[NSArray arrayWithObjects:@"", @"",@"",@"",@"",nil]];
// Do any additional setup after loading the view, typically from a nib. }
到这里初始化就完成了
二:UITableView数据源的初始化
UITableView有三个必须要实现的代理方法
#pragma mark - Table View
//设置section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ;
}
//设置每个section 的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [dataArryList count];
}
//对每个TableViewCell进行操作 UITableView中显示的每一个单元都是一个UITableViewCell对象,其初始化函数initWithStyle:reuseIdentifier:在tableView快速滑动的滑动的过程中,频繁的alloc对象是比较费时的///,于是引入了cell的重用机制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellIdentifier = @"cell"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell== nil) {
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
}
cell.textLabel.text=[dataArryList objectAtIndex:indexPath.row];
return cell;
}
三:插入和删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.dataArryList removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
[self.dataArryList insertObject:@"" atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
四:其他的一些常用操作
//设置UITableView行缩进
-(NSInteger)tableView:(UITableView*)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
return row;
}
//设置cell行间隔的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return ;
}
//设置选中Cell的响应事件
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ }
//设置选中的行所执行的动作
-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
return indexPath;
}
//设置划动cell是否出现del按钮,可供删除数据里进行处理
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
}
//设置删除时编辑状态
-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
}
最后看一下运行的效果
ios开发入门篇(三):UITableView简介的更多相关文章
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...
- iOS开发UI篇—核心动画简介
转自:http://www.cnblogs.com/wendingding/p/3801036.html iOS开发UI篇—核心动画简介 一.简单介绍 Core Animation,中文翻译为核心动画 ...
- iOS开发UI篇—实现UItableview控件数据刷新
iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...
- ios开发入门篇(四):UIWebView结合UISearchBar的简单用法
UIWebView是ios开发中比较常用的一个控件.我们可以用它来浏览网页.打开文档等,今天笔者在这里简单介绍下UIWebView和UISearchBar结合起来的用法,做一个简单的类浏览器. 一: ...
- ios开发入门篇(一):创建工程
突然心血来潮,想写点技术方面的东西,做了ios也有好几年了,就简单的写个ios开发的技术博客,希望有人能用得到. 今天就先从创建一个Hellow World工程开始 一:首先打开xcode然后单击Cr ...
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
一.实现效果 二.实现代码 1.数据模型部分 YYQQGroupModel.h文件 // // YYQQGroupModel.h // 02-QQ好友列表(基本数据的加载) / ...
- iOS开发UI篇—在UItableview中实现加载更多功能
一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据. 二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时候,主页面(主控制器 ...
- ios开发入门篇(二):Objective-C的简单语法介绍
一:面向对象的思想 objective-c与C语言的编程思想不同,C语言是面向过程的编程,而objective-c则是面向对象的编程,所谓面向对象,我个人的理解,就是抽象.将具有一定共同点的实物抽象成 ...
随机推荐
- Hadoop on Mac with IntelliJ IDEA - 3 解决MRUnit - No applicable class implementing Serialization问题
本文讲述在IntelliJ IDEA中使用MRUnit 1.0.0测试Mapper派生类时因MapDriver.withInput(final K1 key, final V1 val)的key参数被 ...
- Xdebug的使用
1.http://www.cnblogs.com/mo-beifeng/articles/2446142.html 2.http://www.cnblogs.com/ximu/articles/200 ...
- PostgreSQL中如何查询在当前的哪个数据库中
[pgsql@localhost bin]$ ./psql -d tester psql () Type "help" for help. tester=# select curr ...
- 数据字符集mysql主从数据库,分库分表等笔记
文章结束给大家来个程序员笑话:[M] 1.mysql的目录:在rpm或者yum安装时:/var/lib/mysql 在编译安装时默许目录:/usr/local/mysql 2.用rpm包安装的MyS ...
- Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- MongoDB笔记--安装篇
安装MongoDB 第一步:下载安装包:官方下载地址←单击此处,如果是win系统,注意是64位还是32位版本的,请选择正确的版本. 第二步:新建目录“D:\MongoDB”,解压下载到的安装包,找到b ...
- C#的空接合运算符 三目运算符
1.空接合运算符:操作数1??操作数2: 2.第一个操作数必须是一个可空类型或引用类型,第二个操作数必须与第一个操作数类型相同,或者可以隐含的转换为第一个操作数的类型: 3.如果第一个操作数不为nul ...
- Gradle Goodness: Set Java Compiler Encoding--转载
原文地址:http://java.dzone.com/articles/gradle-goodness-set-java If we want to set an explicit encoding ...
- MII、RMII、GMII接口的详细介绍
转载:http://blog.csdn.net/reille/article/details/6312156 概述: MII (Media Independent Interface(介质无关接口)或 ...
- The Romantic Hero
Problem Description There is an old country and the king fell in love with a devil. The devil always ...