IOS开发学习笔记026-UITableView的使用
UITableView的简单使用过程
简单介绍
两种样式
UITableViewStylePlain
UITableViewStyleGrouped
数据显示需要设置数据源,数据源是符合遵守协议 <UITableViewDataSource>的类
数据源
dataSource
一些UITableViewDataSource协议里写好的类,这些类有系统自动调用,调用顺序是先设置组再设置行最后设置行内容
设置组section,直接将组返回
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
设置行,直接返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
设置行内容,直接返回UITableViewCell对象
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
设置组标题(头部),返回头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
设置组描述(尾部),返回尾部描述
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
其中indexPath包含两个数据一个是section一个是row,也就是每一行数据的位置,表示第几组第几行
具体用法看下面的代码
1、创建一个UITableView对象,并设置数据源
// 创建一个UITableView
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.dataSource = self; // 设置数据源
[self.view addSubview:tableView]; // 添加到视图
既然数据源是self,那么这个类必须遵守协议:UITableViewDataSource,在这个类扩展上遵守协议即可
@interface SLQViewController () <UITableViewDataSource> // 遵守协议 @end
2、设置组
将待添加数据到数组中
@interface SLQViewController () <UITableViewDataSource> // 遵守协议
{
NSArray *_property; // 属性
NSArray *_location; // 位置
}
在viewDidLoad方法中初始化数组
_property = @[@"红",@"蓝",@"黑"];
_location = @[@"上",@"下",@"左",@"右"];
设置有多少组
// 设置组section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ; // 返回组数
}
3、设置每组多少行
// 设置每组多少行 row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == )
return _property.count;
if (section == ) {
return _location.count;
} return ;
}
4、设置第section组第row行的数据
// 设置行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 创建UITableViewCell对象
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
if (indexPath.section == )
{
cell.textLabel.text = _property[indexPath.row];
}
if (indexPath.section == )
{
cell.textLabel.text = _location[indexPath.row];
} return cell; // 返回
}
5、设置每组头部显示的文字
// 设置每组头部显示文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == )
{
return @"颜色";
}
if (section == )
{
return @"位置";
}
return nil;
}
6、设置每组尾部显示的文字
// 设置每组尾部显示文字
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
if(section == )
{
return @"设置一些颜色属性啦啦啦啦";
}
if (section == )
{
return @"位置属性的设置哈哈哈哈";
}
return nil;
}
运行可以看到结果:

7、代码优化
上面的代码看着可扩展性太差,下面来几个优化版本,直接看代码吧
//
// SLQViewController.m
// UITableView的练习
//
// Created by Christian on 15/5/16.
// Copyright (c) 2015年 slq. All rights reserved.
// #import "SLQViewController.h" //
#define kHeader @"header"
#define kFooter @"footer"
#define kSetting @"setting" @interface SLQViewController () <UITableViewDataSource> // 遵守协议 {
// NSArray *_property;
// NSArray *_location; // 优化1
// NSArray *_allSetting;
// NSArray *_noramlSet;
// NSArray *_moveSet; // 优化2
NSArray *_allInfo; // 内部保存字典
}
@end @implementation SLQViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 创建一个UITableView
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.dataSource = self; // 设置数据源
[self.view addSubview:tableView]; // 添加到视图
//
// _property = @[@"颜色",@"大小",@"透明度"];
// _location = @[@"上",@"下",@"左",@"右"];
// 优化1
// _allSetting = @[
// @[@"颜色",@"大小",@"透明度"],
// @[@"上",@"下",@"左",@"右"],
// ];
// _noramlSet = @[@"设置",@"位置"];
// _moveSet = @[@"常见属性设置啦啦啦啦啦了",@"位移属性设置啊啊啊啊啊啊啊啊"];
// 优化2,保存字典
_allInfo = @[
@{
kHeader : @"颜色",
kFooter : @"颜色属性啦啦啦啦啦啦啦啦啦",
kSetting : @[@"红",@"蓝",@"黑"]
},
@{
kHeader : @"位置",
kFooter : @"位置属性啊啊啊啊啊啊啊啊",
kSetting : @[@"上",@"下",@"左",@"右"]
},
@{
kHeader : @"透明属性",
kFooter : @"透明属性啊啊啊啊啊啊啊啊",
kSetting : @[@"透明",@"半透明",@"不透明"]
}
]; }
// 设置组section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//return 2;
// 优化1
//return _allSetting.count;
// 优化2
return _allInfo.count;
}
// 设置每组多少行 row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// if(section == 0)
// return _property.count;
// if (section == 1) {
// return _location.count;
// }
// 优化1
// return [_allSetting[section] count];
// 优化2
return [_allInfo[section][kSetting] count];
//return 0;
}
// 设置行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// if (indexPath.section == 0)
// {
// cell.textLabel.text = _property[indexPath.row];
// }
// if (indexPath.section == 1)
// {
// cell.textLabel.text = _location[indexPath.row];
// }
// 优化1
//cell.textLabel.text = _allSetting[indexPath.section][indexPath.row];
// 优化2
cell.textLabel.text = _allInfo[indexPath.section][kSetting][indexPath.row];
return cell;
}
// 设置每组头部显示文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// if(section == 0)
// {
// return @"设置";
// }
// if (section == 1)
// {
// return @"位置";
// }
// 优化1
// return _noramlSet[section];
// 优化2
return _allInfo[section][kHeader];
}
// 设置每组尾部显示文字
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
// if(section == 0)
// {
// return @"设置一些常见属性";
// }
// if (section == 1)
// {
// return @"位置属性的设置";
// }
// 优化1
// return _moveSet[section];
// 优化2
return _allInfo[section][kFooter];
}
@end
源代码:
http://pan.baidu.com/s/1hqCLqra
其实还有优化的空间,继续学习。。。。
IOS开发学习笔记026-UITableView的使用的更多相关文章
- iOS开发学习笔记:基础篇
iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...
- ios开发学习笔记(1)
objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...
- iOS开发学习笔记
1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...
- ios开发学习笔记(这里一定有你想要的东西,全部免费)
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- IOS开发学习笔记017-第一个IOS应用
第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...
- (ios开发学习笔记一)ios项目文件结构
转自:http://www.cnblogs.com/macroxu-1982/archive/2012/07/31/2616389.html 下面是单个窗体项目例子,我们从这个项目开始,说明ios项目 ...
- IOS开发学习笔记043-QQ聊天界面实现
QQ聊天界面实现 效果如下: 实现过程: 1.首先实现基本界面 头像使用 UIImageView : 文字消息使用 UIButton 标签使用 UILable :水平居中 所有元素在一个cell中,在 ...
- IOS开发学习笔记042-UITableView总结2
一.自定义非等高的cell 如常见的微博界面,有的微博只有文字,有的有文字和图片.这些微博的高度不固定需要重新计算. 这里简单说一下几种方法.前面的步骤和设置等高的cell一样.现在来 ...
- IOS开发学习笔记041-UITableView总结1
一.UITableView的常用属性 1.分割线 // 分割线 self.tableView.separatorColor = [UIColorredColor]; // 隐藏分割线 self.tab ...
随机推荐
- URL最大长度问题
在http协议中,其实并没有对url长度作出限制,往往url的最大长度和用户浏览器和Web服务器有关,不一样的浏览器,能接受的最大长度往往是不一样的,当然,不一样的Web服务器能够处理的最大长度的UR ...
- ASP.NET的三种开发模式
前言 ASP.NET 是一个免费的Web开发框架,是由微软在.NET Framework框架中所提供的,或者说ASP.NET是开发Web应用程序的类库,封装在System.Web.dll 文件中.AS ...
- TP5.0:的安装与配置
在网址中输入:localhost/安装TP5的文件夹/public/ 入口文件位置:public/index.php: 最新版本中,新建的文件夹是没有模型和视图的,需要自行添加没有的文件: 添加前: ...
- 关于android界面菜单,project显示问题
刚用android studio不久,遇到了一个问题:界面菜单project不小心被关掉了,百度各种搜索无结果.我想大多数人都遇到过类似问题,此类问题其实很简单但是对于初学者来说就有点苦恼了.所以在此 ...
- shp格式数据发布服务:postGIS + postgresql + geoserver
主要流程: ①使用postgresql创建数据库 ②下载安装postgis插件 ③在创建的数据库中使用postgis插件,执行下列语句 CREATE EXTENSION postgis; CREATE ...
- 远程链接mongoDB robomongo
墙裂推荐一个软件robomongo 下载地址:https://robomongo.org/download 最初不用这个软件的时候需要shell链接mongoDB,折腾了半天结果版本不匹配 用robo ...
- 漫谈 Clustering (5): Hierarchical Clustering
系列不小心又拖了好久,其实正儿八经的 blog 也好久没有写了,因为比较忙嘛,不过觉得 Hierarchical Clustering 这个话题我能说的东西应该不多,所以还是先写了吧(我准备这次一个公 ...
- vmware:使用.zip文件在vmware中安装操作系统
问题描述: 之前在vmware中安装系统时,全部都是加载的.iso文件来实现.后面同事给了一个zip包,解压后是".vmdk"等一系列具体的文件.一时间不知道怎么安装系统了,搜网页 ...
- SummerVocation_Learning--java的String 类
java中String属于java.lang的package包,是一个类.代表不可变的字符序列. String类的常见构造方法: String(String original),创建一个对象为orig ...
- Fight Against Traffic -简单dijkstra算法使用
题目链接 http://codeforces.com/contest/954/problem/D 题目大意 n m s t 分别为点的个数, 边的个数,以及两个特殊的点 要求s与t间的距离在新增一条边 ...