一、 TableView

1.1 StoryBoard方式

1.2 nib方式

1.2.1 一般

1.2.2 自定义单元格

1.3 纯代码方式

(1) 简单表视图操作

Step1:

实现协议 2个协议:<UITableViewDataSource,UITableViewDelegate>

Step2:

创建UITableView对象 & 设置表格视图对象的代理与表格数据源的代理

//创建一个区域,用来显示表格

//  CGRect rect = CGRectMake(0, 40, 300, 420);

CGRect rect = CGRectMake(0, 40, 300, 420);

//初始化表格视图

UITableView *tableView = [[UITableView alloc]initWithFrame:rect];

//设置表格视图对象的代理

tableView.delegate = self;

//设置表格视图数据源的代理

tableView.dataSource = self;

[self.view addSubview:tableView];

Step3: 设置单元格高度

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 5;

}

Step4: 初始化和返回表格视图的单元格

//该代理方法,用来初始化和返回表格视图的单元格,是最重要的一个代理方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//创建一个字符串,作为单元格的标识符

static NSString *identifier = @"cellIndentifier";

//单元格的标识符,可以看作是一个重用机制,此方法可以从,所有已经开辟内存的单元格里面,选择一个具有同样标识符的、空闲的单元格

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

//如果没有重复使用的单元格,则创建新的单元格

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

//设置单元格的标题文字内容

cell.textLabel.text = @"Cell title here.";

//设置单元格的文字描述内容

cell.detailTextLabel.text = @"Detail Information here";

}

return cell;

}

(2) 其它操作

a. 设置单元格高度

//该代理方法用来设定单元格的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

  return 80;

 }

b. 设置单元格图标

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *identifier = @"cellIndenfier";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

  if (cell == nil) {

  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

  cell.textLabel.text = @"Cell title here.";

          cell.detailTextLabel.text = @"Detail Information here.";

    //建立一个图片对象,导入第一张图片,作为单元格默认状态的图片

  UIImage *img1 = [UIImage imageNamed:@"alarm1.png"];

          //建立另一张图片对象,导入第二张图片,作为单元格被选中状态的图片

    UIImage *img2 = [UIImage imageNamed:@"alarm2.png"];

          //将第一个图片对象,指定为单元格默认图片

    cell.imageView.image = img1;

  //将第二个图片对象,指定为单元格的高亮图片

  cell.imageView.highlightedImage = img2;

  }

      return cell;

}

c. 设置单元格数据源

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *identifier =@"cellIndentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

//索引路径用来标识单元格在表格中的位置,它有section和row两个属性,前者标识单元格处于第几个段落,后者标识单元格在段落中的第几行

NSInteger num = [indexPath row];

//获取数组中的数据,指定为当前单元格的标题文字.

cell.textLabel.text = [_array objectAtIndex:num];

cell.detailTextLabel.text = @"Detail Information here";

}

return cell;

}

d. 设置单元格背景色

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *identifier =@"cellIndentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

//获取     当前     单元格的行数

NSInteger num = [indexPath row];

//设置单元格的标题文字内容

cell.textLabel.text = @"Title Information";

//设置单元格的描述文字内容

cell.detailTextLabel.text = @"Detail Information here";

if (num == 1) {

//选择第二行单元格,则设置单元格背景色为紫色

[cell setBackgroundColor:[UIColor purpleColor]];

}

else {  //若不是第二行,则设置为另外一种背景

//1.

CGRect rect = CGRectMake(0, 0, 50, 50);

//创建一个视图对象

UIView *view = [[UIView alloc] initWithFrame:rect];

//设置视图对象的背景颜色为褐色

[view setBackgroundColor:[UIColor brownColor]];

//将视图作为单元格的背景视图,这里视图的作用是仅改变单元格的背景色,也可以根据需要,设定一张图片作为背景.

[cell setBackgroundView:view];

//2.

//            [cell setBackgroundColor:[UIColor redColor]];

}

}

return cell;

}

e. 设置单元格Accessory样式

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

if (cell.accessoryType == UITableViewCellAccessoryNone)

cell.accessoryType = UITableViewCellAccessoryCheckmark;

else

cell.accessoryType = UITableViewCellAccessoryNone;

}

f.  删除单元格

//该代理方法,用来设定单元格的编辑方式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

//设置单元格的编辑模式为删除模式

return UITableViewCellEditingStyleDelete;

}

//该代理方法,用来处理单元格编辑结束后的动作

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

//判断是否为删除操作

if (editingStyle == UITableViewCellEditingStyleDelete) {

//获得当前编辑的单元格的行编号

NSInteger num = [indexPath row];

//从数组中将该单元格的内容清楚,以保证数据的一致性

[_array removeObjectAtIndex:num];

//再从表格视图中清楚该单元格

NSArray *indexPaths = [NSArray arrayWithObjects:indexPath, nil];

[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];

}

}

g. 插入单元格

//该代理方法,用来设定单元格的编辑方式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

//设置单元格的编辑模式为插入模式

return UITableViewCellEditingStyleInsert;

}

//该代理方法,用来处理单元格编辑结束后的动作

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

//判断是否为删除操作

if (editingStyle == UITableViewCellEditingStyleInsert) {

//获得当前编辑的单元格的行编号

NSInteger num = [indexPath row];

//从数组中将该单元格的内容清楚,以保证数据的一致性

[_array insertObject:@"Nice day" atIndex:num];

//再往表格视图中插入新单元格

NSArray *indexPaths = [NSArray arrayWithObjects:indexPath, nil];

[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];

}

}

h. 更改单元格顺序

//该代理方法,用来设定单元格的编辑方式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

//设置单元格的编辑方式为编辑模式

return UITableViewCellEditingStyleNone;

}

//用来设定是否允许拖动单元格

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

return YES;

}

//用来响应单元格的移动动作.

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

//获得单元格移动前的位置

NSInteger fromRow = [sourceIndexPath row];

//获得单元格移动后的位置

NSInteger toRow = [destinationIndexPath row];

//获得数组在单元格移动前的对象

id obj = [_array objectAtIndex:fromRow];

//删除数组中单元格移动前位置的对象

[_array removeObjectAtIndex:fromRow];

[_array insertObject:obj atIndex:toRow];

}

二、CollectionView

2.1 StoryBoard方式

2.2 nib方式

1.2.1 一般

1.2.2 自定义单元格

2.3 纯代码方式

iOS - 表格的更多相关文章

  1. iOS表格制作

    由于项目上的需求,需要做一个表格出来,来显示流程状态.刚开始脑子一头雾水,没有一点思路,但是靠着自己的座右铭--“世上无难事,只怕有心人”,克服了所有困难.好,不说了,讲正事. 制作表格,还是需要ta ...

  2. IOS常用框架

    IOS开发中有用的第三方库 #Objective-C中最受瞩目库 [链接](https://github.com/languages​​/Objective-C/most_watched) * [th ...

  3. IOS开发中有用的第三方库

    #Objective-C中最受瞩目库 [链接](https://github.com/languages​​/Objective-C/most_watched) * [three20](https:/ ...

  4. 直接拿来用!最火的iOS开源项目

    1. AFNetworking 在众多iOS开源项目中,AFNetworking可以称得上是最受开发者欢迎的库项目.AFNetworking是一个轻量级的iOS.Mac OS X网络通信类库,现在是G ...

  5. (转)直接拿来用!最火的iOS开源项目(二)

    “每一次的改变总意味着新的开始.”这句话用在iOS上可谓是再合适不过的了.GitHub上的iOS开源项目数不胜数,iOS每一次的改变,总会引发iOS开源项目的演变,从iOS 1.x到如今的iOS 7, ...

  6. IOS常用开源库

    转自:http://www.csdn.net/article/2013-06-18/2815806-GitHub-iOS-open-source-projects-two/1 1. AFNetwork ...

  7. GitHub上非常受开发者欢迎的iOS开源项目(二)

    "每一次的改变总意味着新的开始."这句话用在iOS上可谓是再合适不过的了.GitHub上的iOS开源项目数不胜数,iOS每一次的改变,总会引发iOS开源项目的演变,从iOS 1.x ...

  8. IOS开发常见第三方总结

    链接](https://github.com/languages​​/Objective-C/most_watched) * [three20](https://github.com/facebook ...

  9. iOS开源项目周报0323

    由OpenDigg 出品的iOS开源项目周报第十三期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等. CHIPag ...

随机推荐

  1. .NET连接SAP系统专题:C#调用RFC代码(三)

    本文就说明在C#中如何编写代码来调用SAP中的RFC函数获取数据. 首先需要引用两个NCO3.0的DLL DLL下载地址:http://files.cnblogs.com/mengxin523/SAP ...

  2. CodeForces 164C Machine Programming 费用流

    Machine Programming 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co One remark ...

  3. Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks 字符串水题

    A. Kyoya and Photobooks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  4. IOS网络编程——第三方类库

    IOS网络编程——第三方类库 目录 概述 ASIHttpRequest AFNetworking 其他 概述 ASIHttpRequest AFNetworking 其他

  5. 网络IPC:套接字之数据传输

    既然将套接字端点表示为文件描述符,那么只要建立连接,就可以使用read和write来通过套接字通信.通过在connect函数里设置对方地址,数据报套接字也可以“连接”.在套接字描述符上采用read和w ...

  6. Linux下MySQL使用

    Linux刚安装完并不是安装了全部的MySQL,比如Centos就没有安装mysql-server. 故使用rpm -q mysql会看到明明安装了mysql却用不了. 所以先安装mysql-serv ...

  7. iOS “请在微信客户端打开链接” UIWebview加载H5页面携带session、cookie、User-Agent信息 设置cookie、清除cookie、设置User-Agent

    公司新开的一个项目..内容基本上是加载H5页面显示..当时觉得挺简单的..后来发现自己掉坑里了..一些心理历程就不说了..说这个项目主要用到的知识点吧..也是自己踩得坑. 首先说说..这个项目上的内容 ...

  8. .Net Static 与单例

    Static 关键字作为修饰符可以用于类.方法和成员变量上.其含义是对于整个应用程序生命周期内,访问该修饰符修饰的对象/方法/变量都引用到同一实例(内存地址).但正因如此在多线程下会出现线程安全问题: ...

  9. MySQL数据库还原:路径必须用正斜杠?

    也是术业不精,其实之前也用命令行还原过几次MySQL数据库,但总记不清语法.这不,今天想把另一台电脑上备份的数据库还原过来,结果不停报错,如下图所示: 后来才发现,因为偷懒直接复制的路径名里,用的全是 ...

  10. Android 高级UI设计笔记13:Gallery(画廊控件)之 循环显示图像

    1. 循环显示图像的原理  循环显示有些类似于循环链表,最后一个结点的下一个结点又是第1个结点.循环显示图像也可以模拟这一点. 也许细心的读者从上一节实现的ImageAdapter类中会发现些什么.对 ...