TableView使用及优化
2015-03-26 21:50
190 查看
#import <UIKit/UIKit.h>
@interface MainViewController : UITableViewController
@end
------------------------------------------------------------------------------------------------------
#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
@interface MainViewController : UITableViewController
@end
------------------------------------------------------------------------------------------------------
#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
相关文章推荐
- iOS开发学习-如何优化tableview的使用
- 用泛型来优化 TableView Cells 的使用体验
- mysql下优化表和修复表命令使用说明(REPAIR TABLE和OPTIMIZE TABLE)
- 使用UIAlertView和UITableView来创建一个有用的UIAlertTableView
- xcode 4.3.2 storyboard绘制UITableView为理解storyboard使用和tableview有用
- View 变Table 优化MySql
- iOS学习之分段Table View的使用(Grouped样式表格)
- ASP.NET中使用MultiView和View以及Menu实现Tablecontrol的功能
- TableView使用说明
- 关于使用UILocalizedIndexedCollation 实现tableview的边索引
- Android UI 使用Merge,include和 ViewStub--优化(1)
- Android布局优化几个重要标签的使用include、merge、ViewStub、requestFocus
- 使用EventData()捕获操作对象(包括:新建/修改/删除 Table/Sp/View/Fn)动作
- 网页优化系列三:使用压缩后置viewstate
- 优化 scrollView 性能(tableView)
- Navigation 与 tableview controller 一起使用
- iphone tableView的使用
- iphone tableView的使用(zz)
- android应用开发之ImageView,SeekBar,TableHost,ProgressBar的使用
- [转]UITableViewCell的使用——自定义tableView视图