通过字典和数组和宏的综合运制作 UITableview分组

//

//  ViewController.h

//  UITableview li

//

//  Created by 红珊瑚 on 15/5/11.

//  Copyright (c) 2015 红珊瑚. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController



@end



//

//  ViewController.m

//  UITableview li

//

//  Created by 红珊瑚 on 15/5/11.

//  Copyright (c) 2015 红珊瑚. All rights reserved.

//


#import "ViewController.h"


#define kHeader @"Header"

#define kRenzhong @"renzhong"

#define kFooter @"Footer"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

{

    NSArray *arr1;

    UITableView *tableView;

    UIButton *button;

}

@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    //创建tablebview

    tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    tableView.backgroundColor=[UIColor groupTableViewBackgroundColor];

    

    tableView.delegate=self;

    tableView.dataSource=self;

    [self.view addSubview:tableView];

    

   

    

    // 初始化数据

    arr1=@[

            @{

                kHeader:@"奢侈品网购平台",

                kRenzhong:@[@"奢侈化妆品",@"红酒抢购",@"名人字画",@"新品推建",@"名家墨宝"],

                //kFooter:@"位于欧洲南方,东大西洋,西印度洋。"

             },

            

            @{

                kHeader:@"新品上架",

                kRenzhong:@[@"钻戒 3",@"法国红酒 10",@"豪庭别墅 1000"],

                //kFooter:@"位于中国以东,西太平洋。"

            },

            

            @{

                kHeader:@"热销品",

                kRenzhong:@[@"汗血宝马 2000",@"德国黑啤 90",@"空中花园别墅 90000",@"美国曼哈顿 9000000"],

                //kFooter:@"位于欧洲大陆以西,东大西洋。"

            },

            

            @{

                kHeader:@"本周新店",

                kRenzhong:@[@"分尼雅.庄园",@"雷克萨斯腾兴店",@"碧桂园"],

                //kFooter:@"位于南美洲大陆大陆东部,西大西洋。"

                }


        ];

    

    }


     //一共有多少组

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return arr1.count;

}


    //section组有多少行

-(NSInteger)tableView:(UITableView *)tableView

numberOfRowsInSection:(NSInteger)section

{

    NSDictionary *Zu = arr1[section];     //获取数组 每个section(组)

    NSArray *renzhong = Zu[kRenzhong];    //获取去人种里面的 人数 数组

    

    return  renzhong.count;

}


    //放回每一行显示的内容

-(UITableViewCell *)tableView:(UITableView *)tableView

        cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault

                                                reuseIdentifier:nil];       //初始化cell

    

    //取出的section组第row行的数据

    //取出第section组的arr1  这个国家 renzhong数组 row行的 数据

    NSDictionary *Zu = arr1[indexPath.section];

    NSArray *renzhong = Zu[kRenzhong];

    NSString *text = renzhong[indexPath.row];

    

    cell.textLabel.text=text;    //展示文字数据

    

    //cell添加 按钮 ,给按钮添加图片

    button = [[UIButton alloc] initWithFrame:CGRectMake(220,3,80,45)];

    [cell addSubview:button];

    if (indexPath.row==0)

    {

    [button setImage:[UIImage imageNamed:@"u=3208322942,3756051788&fm=21&gp=0.jpg"] forState:UIControlStateNormal];

    }

    if (indexPath.row==1)

    {

        [button setImage:[UIImage imageNamed:@"u=2446483382,2107478354&fm=21&gp=0.jpg"] forState:UIControlStateNormal];

    }

    if (indexPath.row==2)

    {

        [button setImage:[UIImage imageNamed:@"u=1070902365,2619384777&fm=21&gp=0.jpg"] forState:UIControlStateNormal];

    }

    if (indexPath.row==3)

    {

        [button setImage:[UIImage imageNamed:@"u=2783552916,508796275&fm=21&gp=0.jpg"] forState:UIControlStateNormal];

    }

    if (indexPath.row==4)

    {

        [button setImage:[UIImage imageNamed:@"u=3208322942,3756051788&fm=21&gp=0.jpg"] forState:UIControlStateNormal];

    }

   

   /*

    NSInteger row =indexPath.row;

    UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(50, 0, 200, 44)];

    label.backgroundColor=[UIColor grayColor];

    label.text=[NSString stringWithFormat:@"%@",arr1[row]];

    [cell.contentView addSubview:label];

    */

    return cell;

}


    //section组显示头部标题

- (NSString *)tableView:(UITableView *)tableView

titleForHeaderInSection:(NSInteger)section

{

     NSDictionary *Zu = arr1[section];

    return Zu[kHeader];

}


    //section组显示脚部标题

-(NSString *)tableView:(UITableView *)tableView

titleForFooterInSection:(NSInteger)section

{

   // NSDictionary *Zu= arr1[section];

   // return  Zu[kFooter];

    return  arr1[section][kFooter];

}


   //设置每组中每一行的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 50;

}



@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值