iOS学习4_UITableView的使用
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的使用的更多相关文章
- iOS学习-压缩图片(改变图片的宽高)
压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...
- 【原】iOS学习之事件处理的原理
在iOS学习23之事件处理中,小编详细的介绍了事件处理,在这里小编叙述一下它的相关原理 1.UITouch对象 在触摸事件的处理方法中都会有一个存放着UITouch对象的集合,这个参数有什么用呢? ( ...
- iOS学习笔记——AutoLayout的约束
iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...
- 【原】iOS学习47之第三方-FMDB
将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...
- iOS学习路线图
一.iOS学习路线图 二.iOS学习路线图--视频篇 阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天 学习后目标: ...
- 黑苹果-IOS学习的开始
深知安装黑苹果的不易,在这里写一下关于我的Thinkpad E430c安装黑苹果教程(Mac版本:Yosemite 10.10.4),希望能够帮助有需要的朋友. 首先贴上我的电脑配置报表: ----- ...
- iOS 学习资源
这份学习资料是为 iOS 初学者所准备的, 旨在帮助 iOS 初学者们快速找到适合自己的学习资料, 节省他们搜索资料的时间, 使他们更好的规划好自己的 iOS 学习路线, 更快的入门, 更准确的定位的 ...
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...
- iOS学习资源个人整理
1208更新: http://www.tuyiyi.com 图翼网 https://github.com/Alamofire/Al ...
随机推荐
- 【Nescafé 31】杯NOIP模拟赛
t1 题意:n*m的棋盘上从(1,1)走到(n,m),只能向下或向右,一些格子有老鼠,每个老鼠互不相同,当处于与老鼠有重边的格子时,视为看见了这只老鼠,求到终点看到最少的不同老鼠数. 分析:DP 由于 ...
- Java -- Matrix的一点认识
例如在如下一段代码中: Matrix m = new Matrix(2,3); for(int i=0; i<m.getRowDimension(); i++) { for(int j=0; j ...
- log4j的各种类的配置
log4j看上去像是一种简单的,易配置的日志打印技术.但是实际使用的时候发现,还有各种很相似的日志技术.很多地方的配置一乱就不知道怎么对应了.所以应该把log4j的一切做个简单的分类记录. (一)ja ...
- HDU - 2814 Visible Trees
题意: m*n(1<=m,n<=100000)的森林里,起始点在(1,1),某人从(0,0)点开始看,问能看到多少棵树. 题解: 求出1~x中的每个数与1~y的数中互质的数的总和.用素数筛 ...
- 异或值 xor
题目描述 给出一个 N 个点的带权无向图,要求从 1 号点到 N 号点的一条路径,使得路径上的边 权异或值最大. 输入格式 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M ...
- php56升级后php7 mcrypt_encrypt 报错
mcrypt_encrypt(MCRYPT_BLOWFISH, $passphrase, $data, MCRYPT_MODE_CBC, $iv); openssl_encrypt($data, &q ...
- mysql 导入
1.默认情况下:MySQL导入文件大小有限制的,最大为2M,所以当文件很大时候,直接无法导入,可修改php.ini参数调整: 在php.ini中修改相关参数: 影响MySQL导入文件大小的参数有三个: ...
- bzoj3638 Cf172 k-Maximum Subsequence Sum
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3638 [题解] 看到k<=20就感觉很py了啊 我们用一棵线段树维护选段的过程,能选到 ...
- CodeForces Round #403 (Div.2) A-F
精神不佳,选择了在场外同步划水 没想到实际做起来手感还好,早知道就报名了…… 该打 未完待续233 A. Andryusha and Socks 模拟,模拟大法好.注意每次是先判断完能不能收进柜子,再 ...
- Gradle for Android(二)全局设置、自定义BuildConfig
全局设置 如果有很多项目,可以设置全局来统一管理版本号或依赖库,根目录下build.gradle下: ext { compileSdkVersion = 23 buildToolsVersion = ...