UI018---UITableView使用
来源:互联网 发布:数据库图书管理系统 编辑:程序博客网 时间:2024/06/05 09:08
一、一个简单的英雄展示程序
NJHero.h文件代码(字典转模型)
1 #import <Foundation/Foundation.h> 2 3 @interface NJHero : NSObject 4 /** 5 * 头像 6 */ 7 @property (nonatomic, copy) NSString *icon; 8 /** 9 * 名称10 */11 @property (nonatomic, copy) NSString *name;12 /**13 * 描述14 */15 @property (nonatomic, copy) NSString *intro;16 17 - (instancetype)initWithDict:(NSDictionary *)dict;18 + (instancetype)heroWithDict:(NSDictionary *)dict;19 @end
NJViewController.m文件代码
1 #import "NJViewController.h" 2 #import "NJHero.h" 3 4 @interface NJViewController ()<UITableViewDataSource, UITableViewDelegate> 5 /** 6 * 保存所有的英雄数据 7 */ 8 @property (nonatomic, strong) NSArray *heros; 9 @property (weak, nonatomic) IBOutlet UITableView *tableView;10 11 @end12 13 @implementation NJViewController14 15 #pragma mark - 懒加载16 - (NSArray *)heros17 {18 if (_heros == nil) {19 // 1.获得全路径20 NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];21 // 2.更具全路径加载数据22 NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];23 // 3.字典转模型24 NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];25 for (NSDictionary *dict in dictArray) {26 NJHero *hero = [NJHero heroWithDict:dict];27 [models addObject:hero];28 }29 // 4.赋值数据30 _heros = [models copy];31 }32 // 4.返回数据33 return _heros;34 }35 36 - (void)viewDidLoad37 {38 [super viewDidLoad];39 // 设置Cell的高度40 // 当每一行的cell高度一致的时候使用属性设置cell的高度41 self.tableView.rowHeight = 60;42 self.tableView.delegate = self;43 }44 45 #pragma mark - UITableViewDataSource46 // 返回多少组47 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView48 {49 return 1;50 }51 // 返回每一组有多少行52 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section53 {54 return self.heros.count;55 }56 // 返回哪一组的哪一行显示什么内容57 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath58 {59 // 1.创建CELL60 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];61 // 2.设置数据62 // 2.1取出对应行的模型63 NJHero *hero = self.heros[indexPath.row];64 // 2.2赋值对应的数据65 cell.textLabel.text = hero.name;66 cell.detailTextLabel.text = hero.intro;67 cell.imageView.image = [UIImage imageNamed:hero.icon];68 // 3.返回cell69 return cell;70 }71 #pragma mark - UITableViewDelegate72 /*73 // 当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度74 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath75 {76 if (1 == indexPath.row) {77 return 180;78 }79 return 44;80 }81 */82 83 #pragma mark - 控制状态栏是否显示84 /**85 * 返回YES代表隐藏状态栏, NO相反86 */87 - (BOOL)prefersStatusBarHidden88 {89 return YES;90 }91 @end
实现效果:
代码注意点:
(1)在字典转模型的代码处用下面的代码,为可变数组分配dictArray.count个存储空间,可以提高程序的性能
NSMutableArray *models = [NSMutableArrayarrayWithCapacity:dictArray.count];
(2)设置cell的高度
有三种办法可以设置cell的高度
1) 可以在初始加载方法中设置,self.tableView.rowHeight = 60;这适用于当每一行的cell高度一致的时候,使用属性设置cell的高度。
2)在storyboard中设置,适用于高度一致
3)当每一行的cell的高度不一致的时候就使用代理方法设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (1 == indexPath.row) { return 180; } return 44;}
二、cell的一些属性
代码示例:
1 #import "CBViewController.h" 2 #import "CBHero.h" 3 4 @interface CBViewController ()<UITableViewDataSource, UITableViewDelegate> 5 /** 6 * 保存所有的英雄数据 7 */ 8 @property (nonatomic, strong) NSArray *heros; 9 @property (weak, nonatomic) IBOutlet UITableView *tableView; 10 11 @end 12 13 @implementation NJViewController 14 15 #pragma mark - 懒加载 16 - (NSArray *)heros 17 { 18 if (_heros == nil) { 19 // 1.获得全路径 20 NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"]; 21 // 2.更具全路径加载数据 22 NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath]; 23 // 3.字典转模型 24 NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count]; 25 for (NSDictionary *dict in dictArray) { 26 NJHero *hero = [NJHero heroWithDict:dict]; 27 [models addObject:hero]; 28 } 29 // 4.赋值数据 30 _heros = [models copy]; 31 } 32 // 4.返回数据 33 return _heros; 34 } 35 36 - (void)viewDidLoad 37 { 38 [super viewDidLoad]; 39 // 设置Cell的高度 40 // 当每一行的cell高度一致的时候使用属性设置cell的高度 41 self.tableView.rowHeight = 60; 42 self.tableView.delegate = self; 43 44 } 45 46 #pragma mark - UITableViewDataSource 47 // 返回多少组 48 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 49 { 50 return 1; 51 } 52 // 返回每一组有多少行 53 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 54 { 55 return self.heros.count; 56 } 57 // 返回哪一组的哪一行显示什么内容 58 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 59 { 60 // 1.创建CELL 61 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; 62 // 2.设置数据 63 // 2.1取出对应行的模型 64 NJHero *hero = self.heros[indexPath.row]; 65 // 2.2赋值对应的数据 66 cell.textLabel.text = hero.name; 67 cell.detailTextLabel.text = hero.intro; 68 cell.imageView.image = [UIImage imageNamed:hero.icon]; 69 70 // 2.3设置cell的辅助视图 71 // cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 72 if (0 == indexPath.row) { 73 cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd]; 74 }else 75 { 76 cell.accessoryView = [[UISwitch alloc] init]; 77 } 78 // UIButton *btn = [[UIButton alloc] init]; 79 // btn.backgroundColor = [UIColor redColor]; 80 // cell.accessoryView = btn; 81 82 83 // 2.4设置cell的背景颜色 84 cell.backgroundColor = [UIColor blueColor]; 85 86 // 设置默认状态的背景 87 // UIView *view = [[UIView alloc] init]; 88 // view.backgroundColor = [UIColor blueColor]; 89 // cell.backgroundView = view; 90 91 UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"buttondelete"]]; 92 cell.backgroundView = iv; 93 94 // 设置选中状态的背景 95 UIView *view2 = [[UIView alloc] init]; 96 view2.backgroundColor = [UIColor purpleColor]; 97 cell.selectedBackgroundView = view2; 98 // 3.返回cell 99 return cell;100 }101 102 103 #pragma mark - 控制状态栏是否显示104 /**105 * 返回YES代表隐藏状态栏, NO相反106 */107 - (BOOL)prefersStatusBarHidden108 {109 return YES;110 }111 @end
实现效果
cell的一些属性:
(1)设置cell的辅助视图,设置cell.accessoryView(系统提供了枚举型,也可以自定义@父类指针指向子类对象);
(2)设置cell的背景颜色,有两种方式可以设置cell的背景颜色:
通过backgroundColor 和 backgroundView都可以设置cell的背景。但是backgroundView 的优先级比 backgroundColor的高,所以如果同时设置了backgroundColor和backgroundView, 那么backgroundView会盖住backgroundColor
示例:cell.backgroundColor = [UIColorblueColor];
(3)设置cell默认状态的背景
示例1:
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor blueColor];
cell.backgroundView = view;
示例2:
UIImageView *iv = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"buttondelete"]];cell.backgroundView = iv;(父类指针指向子类对象,可以使用图片用简单的操作设置绚丽的效果)
(4)设置cell选中状态的背景
示例:
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColorpurpleColor];cell.selectedBackgroundView = view2;
三、tableview的一些属性
代码示例:
1 #import "CBViewController.h" 2 3 @interface CBViewController ()<UITableViewDataSource> 4 5 @end 6 7 @implementation CBViewController 8 9 - (void)viewDidLoad10 {11 [super viewDidLoad];12 13 // 1.创建tableview14 UITableView *tableview = [[UITableView alloc] init];15 tableview.frame = self.view.bounds;16 17 // 2.设置数据源18 tableview.dataSource =self;19 20 // 3.添加tableview到view21 [self.view addSubview:tableview];22 23 // 4.设置分割线样式24 // tableview.separatorStyle = UITableViewCellSeparatorStyleNone;25 26 // 5.设置分割线颜色27 接收的参数是颜色的比例值28 tableview.separatorColor = [UIColor colorWithRed:0/255.0 green:255/255.0 blue:0/255.0 alpha:255/255.0];29 30 // 设置tableview的头部视图31 tableview.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];32 tableview.tableFooterView = [[UISwitch alloc] init];33 }34 35 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView36 {37 return 1;38 }39 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section40 {41 return 10;42 }43 44 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath45 {46 // 1.创建cell47 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];48 49 // 2.设置cell的数据50 cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row ];51 52 // 3.返回cell53 return cell;54 }55 56 - (BOOL)prefersStatusBarHidden57 {58 return YES;59 }60 @end
实现效果:
tableview的一些属性:
(1)设置分割样式(tableview.separatorStyle),这是个枚举类型
(2)设置分割线的颜色,可以直接使用系统给出的颜色,如果系统给定的颜色不能满足需求时,也可以自定义。
补充:颜色分为24位和32位的,如下
24bit颜色
R 8bit 0 ~ 255 G 8bit 0 ~ 255 B 8bit 0 ~ 255 32bit颜色 A 8bit 0 ~ 255(tou) R 8bit G 8bit B 8bit #ff ff ff 白色 #00 00 00 黑色 #ff 00 00 红色 #255 00 00
设置为自定义颜色的实例
tableview.separatorColor = [UIColorcolorWithRed:0/255.0green:255/255.0blue:0/255.0alpha:255/255.0];
//接收的参数是颜色的比例值
(3)设置顶部和底部视图
tableview.tableHeaderView //顶部
tableview.tableFooterView //底部
- UI018---UITableView使用
- UITableView使用
- UITableView使用
- UITableView使用
- UITableView使用
- UITableView使用
- UITableView delegate的使用
- UITableView 使用心得
- UITableView 使用总结
- UITableView的简单使用
- UITableView的使用大全
- UITableView的使用
- UITableView的使用大全
- 构造和使用UITableView
- UITableView的使用大全
- UITableView的使用大全
- UITableView的使用
- UITableView生活使用
- poj 2002 正方形个数 (对点的hash 存储)
- 中文乱码辨识
- intellij idea 修改背景保护色&&修改字体&&快捷键大全
- Windows/Linux远程桌面
- POJ_2352_Stars
- UI018---UITableView使用
- PBOC交易过程详解
- delphi之模拟点击网页中的按钮
- HDOJ1394 Minimum Inversion Number(线段树)
- 命名管道客户端及服务器端简单代码示例
- UI019---ITableviewcell的性能问题和缓存优化
- 搜索引擎之路(一)--- 《搜索引擎---信息检索实践》(读书笔记)
- POJ_1470_ClosestCommonAncestors
- 算法题:求二进制位中0的个数