首页 / IOS / IOS中tableView的数据优化
IOS中tableView的数据优化
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了IOS中tableView的数据优化,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3194字,纯文字阅读大概需要5分钟。
内容图文
![IOS中tableView的数据优化](/upload/InfoBanner/zyjiaocheng/1243/5dee1ec90d9a4be4ac1fee354b7c78a4.jpg)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
#import "MainViewController.h"
@interface
MainViewController ()
@end
@implementation
MainViewController
#pragma mark 实例化视图
- (
void
)loadView
{
self
.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStylePlain];
}
#pragma mark - 数据源方法
#pragma mark 分数数量
- (
NSInteger
)numberOfSectionsInTableView:(UITableView *)tableView
{
return
3;
}
#pragma mark 数据行数
- (
NSInteger
)tableView:(UITableView *)tableView numberOfRowsInSection:(
NSInteger
)section
{
if
(0 == section) {
return
10;
}
else
if
(1 == section) {
return
20;
}
else
{
return
15;
}
}
#pragma mark 表格行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(
NSIndexPath
*)indexPath
{
// 表格行每次出现都会被实例化一次,对系统的负担会比较大
// 所谓可重用标示符,是用来在缓冲池查找缓冲单元格使用的字符串
// 1)指定可重用标示符字符串内容
static
NSString
*CellID = @
"MyCell"
;
// 2) 让tableView去缓冲池查找是否有可重用的表格行
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
// 3) 如果没有找到,则使用可充用标示符实例化新的单元格对象
if
(cell ==
nil
) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
NSLog
(@
"实例化单元格 %@"
, indexPath);
}
// 设置表格内容
NSString
*str = [
NSString
stringWithFormat:@
"itcast-%02d组-%03d行"
, indexPath.section, indexPath.row];
[cell.textLabel setText:str];
return
cell;
}
#pragma mark - 标题行
// 如果是字符串形式,本身不需要进行优化,因为通常会有一个数据字典维护,要显示的字符串,直接通过section从数组中提取即可
- (
NSString
*)tableView:(UITableView *)tableView titleForHeaderInSection:(
NSInteger
)section
{
return
[
NSString
stringWithFormat:@
"第%d组"
, section];
}
#pragma mark 自定义视图标题行
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(
NSInteger
)section
{
// 使用可重用标示符才能做到表格行的优化
// 要实现标题行的优化,需要实例化:UITableViewHeaderFooterView
// 1) 指定static可重用标示符
static
NSString
*HeaderID = @
"MyHeader"
;
// 2) 使用可重用标示,让表格在缓冲区查找是否存在可重用的标题行视图
UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderID];
// 3) 如果没有找到缓存的标题行视图,则使用可重用标示符实例化新的表格行视图
if
(header ==
nil
) {
// 必须要使用initWithReuseIdentifier方法,实例化UITableViewHeaderFooterView视图
header = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:HeaderID];
[header setFrame:CGRectMake(0, 0,
self
.tableView.bounds.size.width, 44)];
NSLog
(@
"实例化标题行 %d"
, section);
// 自定义标题栏视图内容部分的代码,需要写在 header == nil 分支语句中
// 以下代码实际上在通过代码编写自定义视图时的,视图初始化方法的内容
// 注意:对于自定义控件的代码,不要写在分支语句的外部,否则就到不到优化的效果了
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@
"hello"
forState:UIControlStateNormal];
[button setFrame:header.bounds];
[header addSubview:button];
}
NSLog
(@
"标题行视图的子视图数量:%d"
, header.subviews.count);
return
header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(
NSInteger
)section
{
return
44;
}
#pragma mark - 页脚明细
@end
|
原文:http://www.cnblogs.com/inandroid/p/3648294.html
内容总结
以上是互联网集市为您收集整理的IOS中tableView的数据优化全部内容,希望文章能够帮你解决IOS中tableView的数据优化所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。