IOS中tableView的数据优化

 

#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

 

posted @ 2014-04-06 10:44  andy_shen  阅读(924)  评论(0编辑  收藏  举报