UITableView相当于Android里面的ListView。但功能却比ListView强大太多。

使用UITableView须要指定数据源和代理。

1.显示全部的行

遵守UITableViewDataSource协议,必须实现的方法有两个:

// 每一节里面有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// 每行的View,这里是UITableViewCell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

2.改动每行高度选中行

遵守UITableViewDelegate协议

// 选中某行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// 设置每行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

3.编辑模式

UITableView设置编辑模式能够(滑动)删除、加入和移动每一行。仅仅须要改动其属性editing

@property(nonatomic,getter=isEditing) BOOL editing;

须要实现的方法

// 删除须要实现方法
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
// 移动须要实现的方法
- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

4.性能优化

首先依据Identifier从可重用的队列中拿。假设没有再又一次分配Cell的内存。

static NSString *ID =@"tableview";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ID];
}

5.样例

模型类:

Shop.h

@interface Shop : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic) BOOL isChecked;
+ (id)shopWithName:(NSString *)name icon:(NSString *)icon desc:(NSString *)desc;
@end

Shop.m

#import "Shop.h"
@implementation Shop
+ (id)shopWithName:(NSString *)name icon:(NSString *)icon desc:(NSString *)desc
{
Shop *shop = [[Shop alloc] init];
shop.icon = icon;
shop.name = name;
shop.desc = desc;
return shop;
}
@end

自己定义UITableViewCell

QhMyTableViewCell.h

@class Shop;
@interface QhMyTableViewCell : UITableViewCell
+ (QhMyTableViewCell *) myTableViewCell;
@property (weak, nonatomic) IBOutlet UIImageView *imageViews;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *desc;
@property (nonatomic) Shop * shop;
@end

QhMyTableViewCell.m

@implementation QhMyTableViewCell
+ (QhMyTableViewCell *) myTableViewCell
{
NSArray * views = [[NSBundle mainBundle] loadNibNamed:@"cell" owner:nil options:nil];
QhMyTableViewCell * view = views[0];
return view;
}
#pragma mark -重写set方法。设置数据
- (void)setShop:(Shop *)shop
{
self.imageView.image = [UIImage imageNamed:[shop icon]];
self.title.text = [shop name];
self.desc.text = [shop desc];
}
@end

QhViewController.h

@interface QhViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *name;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *deleteIcon;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *allPick; //删除/编辑/反选按button的动作
- (IBAction)remove:(UIBarButtonItem *)sender;
- (IBAction)edit:(UIBarButtonItem *)sender;
- (IBAction)allPickAction:(id)sender; // 使用载入xib然后通过连线的方式找到子控件进行设置,file owner设置为该控制器类时的输出口
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *titles;
@property (weak, nonatomic) IBOutlet UILabel *descs; @end

QhViewController.m

@interface QhViewController () <UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray * _shops;
BOOL _flag;
} @end @implementation QhViewController - (void)viewDidLoad
{
[super viewDidLoad];
_shops = [NSMutableArray array];
Shop *shop1 = [Shop shopWithName:@"111" icon:@"001.png" desc:@"111。111。111"];
Shop *shop2 = [Shop shopWithName:@"222" icon:@"002.png" desc:@"222,222,222"];
Shop *shop3 = [Shop shopWithName:@"333" icon:@"003.png" desc:@"333,333。333"];
Shop *shop4 = [Shop shopWithName:@"444" icon:@"004.png" desc:@"444。444。444"];
Shop *shop5 = [Shop shopWithName:@"555" icon:@"005.png" desc:@"555。555。555"];
Shop *shop6 = [Shop shopWithName:@"666" icon:@"006.png" desc:@"666。666。666"];
Shop *shop7 = [Shop shopWithName:@"777" icon:@"007.png" desc:@"777,777,777"];
Shop *shop8 = [Shop shopWithName:@"888" icon:@"008.png" desc:@"888,888。888"];
Shop *shop9 = [Shop shopWithName:@"666" icon:@"006.png" desc:@"666,666。666"];
Shop *shop10 = [Shop shopWithName:@"777" icon:@"007.png" desc:@"777,777。777"];
Shop *shop11 = [Shop shopWithName:@"888" icon:@"008.png" desc:@"888,888,888"];
Shop *shop12 = [Shop shopWithName:@"666" icon:@"006.png" desc:@"666。666,666"];
Shop *shop13 = [Shop shopWithName:@"777" icon:@"007.png" desc:@"777,777,777"];
Shop *shop14 = [Shop shopWithName:@"888" icon:@"008.png" desc:@"888,888。888"];
[_shops addObjectsFromArray:@[shop1, shop2, shop3,shop4,shop5,shop6,shop7,shop8,shop9,shop10,shop11,shop12,shop13,shop14]];
} //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
// return 1;
//} #pragma mark 每一节里面有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 刷新数据时更改反选button的状态。没有数据时为不可选种状态
if (_shops.count > 0) {
[_allPick setEnabled:YES];
}
else
[_allPick setEnabled:NO];
// 记录选中的行数
NSInteger i = 0;
for (Shop * shop in _shops) {
if (shop.isChecked) {
i++;
}
}
NSString * nameString;
if (i>0) {
nameString = [NSString stringWithFormat:@"已勾选(%d)",i];
}else
nameString = @"已勾选";
self.name.title = nameString;
// 仅仅要有一行为选中状态就让删除button为可选状态
_flag = NO;
for (Shop * shop in _shops) {
if (shop.isChecked) {
_flag = YES;
}
}
if (_flag) {
[_deleteIcon setEnabled:YES];
}else
[_deleteIcon setEnabled:NO];
return _shops.count; } #pragma mark- 每行显示的cell
#pragma mark 1表示使用系统自带的cell作为每一行的布局。 #pragma mark 2表示载入自己定义的xib文件,然后通过顺序或者tag找到每一个子控件赋值。
#pragma mark 3表示载入xib然后通过连线的方式找到子控件进行设置。file owner设置为控制器类。
#pragma mark 终于採用的方式是自己定义UITableViewCell。将xib相应的类改动为自己的类。连线。fileowner设置为nil。
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"tableview";
//1 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
QhMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
//1 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
//2 NSArray * views = [[NSBundle mainBundle] loadNibNamed:@"cell" owner:nil options:nil];
//2 cell = views[0]; //3 NSArray * views = [[NSBundle mainBundle] loadNibNamed:@"cell" owner:self options:nil];
//3 cell = views[0];
cell = [QhMyTableViewCell myTableViewCell];
}
NSLog(@"----->%p", cell);
//2 UIImageView * imageView = cell.contentView.subviews[0];
//2 UILabel * title = cell.contentView.subviews[1];
//2 UILabel * desc = cell.contentView.subviews[2];
//3 不须要。由于已经通过连线定义了输出口 Shop * shop = _shops[indexPath.row];
//1 cell.imageView.image = [UIImage imageNamed:shop.icon];
//1 cell.textLabel.text = [shop name];
//1 cell.detailTextLabel.text = [shop desc]; //2 imageView.image = [UIImage imageNamed:shop.icon];
//2 title.text = [shop name];
//2 desc.text = [shop desc]; //3 _imageView.image = [UIImage imageNamed:shop.icon];
//3 _titles.text = [shop name];
//3 _descs.text = [shop desc]; [cell setShop:shop];
if (shop.isChecked) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger clickIndex = indexPath.row;
Shop * shop = _shops[clickIndex];
BOOL b = shop.isChecked;
shop.isChecked = !b;
[_tableView reloadData];
} #pragma mark 删除button的动作
- (IBAction)remove:(UIBarButtonItem *)sender
{
int i = 0;
for (;i < _shops.count;) {
Shop * shop = _shops[i];
if (shop.isChecked) {
[_shops removeObjectAtIndex:i];
i = 0;
}else i ++;
}
[_tableView reloadData];
} #pragma mark 反选button的动作
- (IBAction)allPickAction:(id)sender {
for (Shop * shop in _shops) {
BOOL b = shop.isChecked;
shop.isChecked = !b;
}
[_tableView reloadData];
} #pragma mark 编辑button的动作
- (IBAction)edit:(UIBarButtonItem *)sender {
//_tableView.editing = !self.tableView.editing;
[_tableView setEditing:!self.tableView.editing animated:YES];
} #pragma mark 删除相应的行 滑动删除的方法
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 从数据源删除
[_shops removeObjectAtIndex:indexPath.row]; // 刷新数据
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
} #pragma mark 编辑模式下移动行要实现的方法
- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
Shop * s = _shops[sourceIndexPath.row];
// 先删除源
[_shops removeObjectAtIndex:sourceIndexPath.row];
// 再插入到目的
[_shops insertObject:s atIndex:destinationIndexPath.row];
// 刷新数据
}
@end

在- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section方法中的数字表示使用了四种方式达到同样的目的。

方式2和Android里面的思路一致。

终于採用的方式能够实现松耦合有利于代码以后的又一次利用。

1表示使用系统自带的cell作为每一行的布局。

2表示载入自己定义的xib文件,然后通过顺序或者tag找到每一个子控件赋值。

3表示载入xib然后通过连线的方式找到子控件进行设置,file owner设置为控制器类。

终于採用的方式是自己定义UITableViewCell。将xib相应的类改动为自己的类,连线。fileowner设置为nil。

iOS学习4_UITableView的使用的更多相关文章

  1. iOS学习-压缩图片(改变图片的宽高)

    压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...

  2. 【原】iOS学习之事件处理的原理

    在iOS学习23之事件处理中,小编详细的介绍了事件处理,在这里小编叙述一下它的相关原理 1.UITouch对象 在触摸事件的处理方法中都会有一个存放着UITouch对象的集合,这个参数有什么用呢? ( ...

  3. iOS学习笔记——AutoLayout的约束

    iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...

  4. 【原】iOS学习47之第三方-FMDB

    将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...

  5. iOS学习路线图

    一.iOS学习路线图   二.iOS学习路线图--视频篇       阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天       学习后目标:    ...

  6. 黑苹果-IOS学习的开始

    深知安装黑苹果的不易,在这里写一下关于我的Thinkpad E430c安装黑苹果教程(Mac版本:Yosemite 10.10.4),希望能够帮助有需要的朋友. 首先贴上我的电脑配置报表: ----- ...

  7. iOS 学习资源

    这份学习资料是为 iOS 初学者所准备的, 旨在帮助 iOS 初学者们快速找到适合自己的学习资料, 节省他们搜索资料的时间, 使他们更好的规划好自己的 iOS 学习路线, 更快的入门, 更准确的定位的 ...

  8. iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...

  9. iOS学习资源个人整理

    1208更新: http://www.tuyiyi.com                                    图翼网 https://github.com/Alamofire/Al ...

随机推荐

  1. 一个自动安装LNMP的简洁Shell脚本

    此脚本在生产服务器上使用了一年多,本脚本崇尚简单唯美,只需要一个脚本就可以在任何一台有网络的服务器上自动配置LNMP.本脚本会在脚本执行目录下,建packages目录用于存放LNMP所需要的软件.大家 ...

  2. WordPress多本小说主题–WNovel主题发布,十分钟搭建小说站! 现已更新至1.2版本

    本文属于<WNovel主题操作手册>文章系列,该系列共包括以下 8 部分: WNovel主题使用手册之–主题安装及更新教程 WNovel主题使用手册之–小说管理 WNovel主题使用手册之 ...

  3. 【bzoj4719】[Noip2016]天天爱跑步 权值线段树合并

    题目描述 给出一棵n个点的树,以及m次操作,每次操作从起点向终点以每秒一条边的速度移动(初始时刻为0),最后对于每个点询问有多少次操作在经过该点的时刻为某值. 输入 第一行有两个整数N和M .其中N代 ...

  4. poj 1062 昂贵的聘礼 (最短路径)

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33365   Accepted: 9500 Descriptio ...

  5. 染色 color

    染色 color 题目描述 有一块矩阵平板,分成n*m个格子,一开始全是白色.在这上面进行k次染色,每次染色按照如下步骤:1. 随机选择一个格子,称为A.2. 随机选择一个格子,称为B.3. 将由A ...

  6. HTML标签的使用要注意语义化

    语义化标签:你认为用什么标签最能描述这块内容,觉得这样表述更有意义,那么就可以使用这个标签. 现在的浏览器对CSS支持都挺完善的(不包括CSS3),讲究的是结构与表现相分离,结构与行为相分离,一个WE ...

  7. Tomcat学习笔记(四)

    Servlet容器部分 servlet容器用来处理请求servlet资源,并为web客服端填充response对象模块,在tomcat中,共有4种类型的容器,分别是:Engine.Host.Conte ...

  8. 2.1 Python3.5安装以及爬虫需要的环境配置

    之所以选用Python,是因为对于网络爬虫来说,Python是最好上手的一种语言.本文讲述的安装配置都是基于Windows的环境. 另外我想说的是,文中用到的下载链接尽量官方网站上的下载链接,这是我比 ...

  9. Lesson9 some interesting things in C#

    1.关键帧动画 1)xml 界面 <Page x:Class="Test.MainPage" xmlns="http://schemas.microsoft.com ...

  10. 创建型设计模式之建造者模式(Builder)

    结构 意图 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 适用性 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时. 当构造过程必须允许被构造的对象有不 ...