最近做项目又开始用到了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简介的更多相关文章

  1. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)

    iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...

  2. iOS开发UI篇—核心动画简介

    转自:http://www.cnblogs.com/wendingding/p/3801036.html iOS开发UI篇—核心动画简介 一.简单介绍 Core Animation,中文翻译为核心动画 ...

  3. iOS开发UI篇—实现UItableview控件数据刷新

    iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...

  4. iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建

    iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...

  5. ios开发入门篇(四):UIWebView结合UISearchBar的简单用法

     UIWebView是ios开发中比较常用的一个控件.我们可以用它来浏览网页.打开文档等,今天笔者在这里简单介绍下UIWebView和UISearchBar结合起来的用法,做一个简单的类浏览器. 一: ...

  6. ios开发入门篇(一):创建工程

    突然心血来潮,想写点技术方面的东西,做了ios也有好几年了,就简单的写个ios开发的技术博客,希望有人能用得到. 今天就先从创建一个Hellow World工程开始 一:首先打开xcode然后单击Cr ...

  7. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)

    一.实现效果             二.实现代码 1.数据模型部分 YYQQGroupModel.h文件 // // YYQQGroupModel.h // 02-QQ好友列表(基本数据的加载) / ...

  8. iOS开发UI篇—在UItableview中实现加载更多功能

    一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据.                      二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时候,主页面(主控制器 ...

  9. ios开发入门篇(二):Objective-C的简单语法介绍

    一:面向对象的思想 objective-c与C语言的编程思想不同,C语言是面向过程的编程,而objective-c则是面向对象的编程,所谓面向对象,我个人的理解,就是抽象.将具有一定共同点的实物抽象成 ...

随机推荐

  1. Oracle数据库的版本变迁功能对比

    Oracle数据库自发布至今,也经历了一个从不稳定到稳定,从功能简单至强大的过程.从第二版开始,Oracle的每一次版本变迁,都具有里程碑意义. 1979年的夏季,RSI(Oracle公司的前身,Re ...

  2. MySQL常用查询语句集合《转》

    一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!>,!<,=>,= ...

  3. ssh 设置免password登录

    如果: 实现->操作机A机 要以用户"user1"身份.免password登录B机和C机? B机和C机 建立用户 user1 而且能够ssh 登录. A机设置: 1.安装ss ...

  4. 学习JSONP

    最近自己研究 跨域调用js,然后 发现 有jsonp 这种技术,在Jquery中可以使用,于是 研究下原理 发现: 其实 就是 利用<script>的跨域访问的能力. 调用 服务端 返回的 ...

  5. 【WinForm】使用NSIS发布程序

    简介 NSIS(Nullsoft Scriptable Install System)是一个开源的 Windows 系统下安装程序制作程序.它提供了安装.卸载.系统设置.文件解压缩等功能 使用 以下是 ...

  6. Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

    Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...

  7. delphi 为应用程序添加提示

    type  TForm1 = class(TForm)    Button1: TButton;    Panel1: TPanel;    Edit1: TEdit;    procedure Fo ...

  8. 发布站点到远程FTP根目录

    第一步:修改 phpcms/modules/admin/templates/site_add.tpl.php 找到 <legend><?php echo L('release_poi ...

  9. WWDC2015—图解

       

  10. gradle使用小记

    1.全局排除依赖: allprojects {    apply plugin: 'java'    apply plugin: 'eclipse'    apply plugin: 'maven-p ...