//
// 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