iphone开发之表格组件UITableView的使用(二)如何分组展示数据并添加组头和组尾描述...

1、在实现UITAbleView数据源协议即UITableViewDataSource内的如下方法中:
// 此方法用于告诉哪个UITableView的每一组的每一行显示什么内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
可以设置单元格的样式Style,当为如下所示
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];单元格的样式为Default时只显示一行文字。当它为别的样式例如:
UITableViewCellStyleSubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
样式时可以显示图片框,和两个文字Label。其中
cell.textLable.text = @“XXX是用来显示单元格图片框右侧最上面的大Label。而大Label下面小的Label则用如下方法进行设置,例如:cell.detailTextLabel.text =……
cell.imageView.iamge = [UIimage imageNameNamed:@“xxx.png”]; 用来设置ImageView的图片
2、 如何实现分组展示数据:
(1)因为默认是plain不分组,所以要设置UITableView的格式为分组格式。
(2)如何显示组标题
在遵守数据源协议的对象中添加如下方法并进行实现:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section;
——tableView:此参数是用来根据tag判断当前是哪个UITableView组件的。
——section:第几组。此参数是当前tag的UITableView组件的第几组,根据不同的section 即不同组分别返回不同的组标题。
(3)如何显示组描述
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
参数用法与以上相同,根据不同UITableView控件的不同组返回不同的组描述。
注意:在手动添加方法时有以下技巧:
(1)按住command键,进入相应协议寻找要添加的具体方法。

(2)先写出方法的返回类型,再进行实现。

代码验证示例如下:

编辑控制器的.h文件如下:

//
//  ViewController.h
//  UITableView展示分组数据
//
//  Created by apple on 15/8/30.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController  <UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@end
编辑控制器的.m文件如下:

//
//  ViewController.m
//  UITableView展示分组数据
//
//  Created by apple on 15/8/30.
//  Copyright (c) 2015年 LiuXun. All rights reserved.
//

#import "ViewController.h"
#define  WIDTH  [UIScreen mainScreen].bounds.size.width
#define  HEIGHT  [UIScreen mainScreen].bounds.size.height
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    
    [super viewDidLoad];
	self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped]; // 分配一个充满全屏,且为分组格式的UITableView空间
    self.tableView.dataSource = self;  // 为此UITableView控件设置数据源为当前控制器对象
    [self.view addSubview:self.tableView];  // 将UITableView控件加到控制器的View中进行显示
    
}

//  以下实现数据源协议中的方法

// 方法一:告诉分为几个组
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;  // 为UItableView组件分为3个组
}

// 方法二:每个组分为多少行
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {  //为第一组分配3行
        return 3;
    }else if(section == 1)  // 为第二组分配2行
    {
        return 2;
    }else{   // 为第三组分配1行
        return 1;
    }
}

//  方法三:告诉当前UITableView控件每一组的每一行单元格显示什么内容,返回一个单元格对象
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    
    if (indexPath.section == 0) {  // 如果当前是第一组
        if (indexPath.row == 0 ) { //如果为当前组的第一行
            cell.textLabel.text = @"中国";  // 设置第一行的大标题
            cell.detailTextLabel.text = @"历史悠久的国家"; // 设置第二行的小标题
            cell.imageView.image = [UIImage imageNamed:@"7.png"];  // 设置ImageView
        }else if (indexPath.row == 1){ // 如果为当前组的第二行
            cell.textLabel.text = @"日本";  //设置上面的大标题
            cell.detailTextLabel.text = @"非常小的国家"; // 设置下面的小标题
            cell.imageView.image = [UIImage imageNamed:@"2.png"];
        }else{ //  如果为当前组的第三行
            cell.textLabel.text = @"朝鲜";
            cell.detailTextLabel.text = @"非常小的国家"; // 设置下面的小标题
            cell.imageView.image = [UIImage imageNamed:@"3.png"];
        }
    }else if (indexPath.section == 1){  // 如果当前是第二组
        if (indexPath.row == 0 ) { //如果为当前组的第一行
            cell.textLabel.text = @"美国";
            cell.detailTextLabel.text = @"非常富有的国家"; // 设置下面的小标题
            cell.imageView.image = [UIImage imageNamed:@"4.png"];
        }else{  // 如果为当前组的第二行
            cell.textLabel.text = @"英国";
            cell.detailTextLabel.text = @"非常有文化的国家"; // 设置下面的小标题
            cell.imageView.image = [UIImage imageNamed:@"5.png"];
        }
    }else{ //如果当前是第三组
        cell.textLabel.text = @"东非";
        cell.detailTextLabel.text = @"非常穷的国家"; // 设置下面的小标题
        cell.imageView.image = [UIImage imageNamed:@"6.png"];
    }
    return cell;
}

// 方法四:为每一组添加组头(头标题)
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title;
    
    if (section == 0) { // 如果为当前UITableView组件的第一组
        title = @"亚洲";
    }else if (section == 1){// 如果为当前UITableView组件的第二组
        title = @"欧洲";
    }else{// 如果为当前UITableView组件的第三组
        title = @"非洲";
    }
    
    return title;
}

// 方法五:为每一组添加组结尾描述
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    NSString *footer;
    if (section == 0) { // 如果为当前UITableView组件的第一组
        footer = @"亚洲是个人口众多的地方";
    }else if (section == 1){// 如果为当前UITableView组件的第二组
        footer = @"欧洲是个非常发达的地方";
    }else{// 如果为当前UITableView组件的第三组
        footer = @"非洲是个非常缺水的地方";
    }
    return footer;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
运行结果如下:






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值