// // ViewController.m // tableviegroup // // Created by ganchaobo on 13-7-2. // Copyright (c) 2013年 ganchaobo. All rights reserved. // #import "ViewController.h" #import "Person.h" @interface ViewController () @property(nonatomic,retain)NSMutableArray *mydata; @end @implementation ViewController #pragma mark -生命周期方法 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UITableView *tableview=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; tableview.dataSource=self; tableview.delegate=self; [self.view addSubview:tableview]; [tableview release]; self.mydata=[NSMutableArray array]; [self InitData1]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)viewDidUnload{ [super viewDidUnload]; self.mydata=nil; } - (void)dealloc { self.mydata=nil; [super dealloc]; } //数据类--》数组--(包含数组)--(每一个数组中只包含多个字典(并且每个字典只有一个key--value) #pragma mark -初始化数据 -(void)InitData1{ //最外面的数组 for (int i=0; i<2; i++) { NSMutableArray *arr1=[NSMutableArray array]; for (int j=0; j<3; j++) { NSString *key=[NSString stringWithFormat:@"itcast-->%zi",j]; NSDictionary *dic=@{key:@(i)}; [arr1 addObject:dic]; } [self.mydata addObject:arr1]; } } 第一种是字典 //一般数据和控件没有太大直接关系 #pragma mark -uitableview 代理方法 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.mydata.count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSArray *sectionq= self.mydata[section]; return sectionq.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIndetify=@"cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIndetify]; if(cell==nil){ cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetify]; } NSArray *section=self.mydata[indexPath.section]; NSDictionary *row=section[indexPath.row]; cell.textLabel.text=row.allKeys[0] ; cell.detailTextLabel.text=[NSString stringWithFormat:@"%zi",[row.allValues[0] intValue]]; return cell; } @end
第二种模型
#import <Foundation/Foundation.h> @interface Person : NSObject @property(nonatomic,copy)NSString *name; @property(nonatomic,copy)NSString *dec; @end // // ViewController.m // tableviegroup // // Created by ganchaobo on 13-7-2. // Copyright (c) 2013年 ganchaobo. All rights reserved. // #import "ViewController.h" #import "Person.h" @interface ViewController () @property(nonatomic,retain)NSMutableArray *mydata; @end @implementation ViewController #pragma mark -生命周期方法 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UITableView *tableview=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; tableview.dataSource=self; tableview.delegate=self; [self.view addSubview:tableview]; [tableview release]; self.mydata=[NSMutableArray array]; [self InitData2]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)viewDidUnload{ [super viewDidUnload]; self.mydata=nil; } - (void)dealloc { self.mydata=nil; [super dealloc]; } -(void)InitData2{ //最外面的数组 for (int i=0; i<2; i++) { NSMutableArray *arr1=[NSMutableArray array]; for (int j=0; j<3; j++) { Person *ps=[[Person alloc] init]; ps.name=[NSString stringWithFormat:@"itcast-->%zi",j]; ps.dec=[NSString stringWithFormat:@"%zi",j]; [arr1 addObject:ps]; [ps release]; } [self.mydata addObject:arr1]; } } //一般数据和控件没有太大直接关系 #pragma mark -uitableview 代理方法 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.mydata.count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSArray *sectionq= self.mydata[section]; return sectionq.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIndetify=@"cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIndetify]; if(cell==nil){ cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetify]; } NSArray *section=self.mydata[indexPath.section]; Person *row=section[indexPath.row]; cell.textLabel.text=row.name; cell.detailTextLabel.text=row.dec; return cell; } @end