Finally:
Way 1, using a dictionary array
BeyondViewController.h
beyondviewcontroller.h// 10_tableview//// Created by Beyond on 14-7-25.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> @interface beyondviewcontroller:uiviewcontroller@end
Beyondviewcontroller.m
beyondviewcontroller.m//10_tableview////Created by Beyond on 14-7-25.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "BeyondViewController.h" #define Kheader @ "header" #define Kfooter @ "Footer" #define Kgirlsarr @ "Girls" @interface Beyondviewcontroller () <uitableviewdatasource>{//False data Nsarray *_array;} @end @implementation beyondviewcontroller-(void) viewdidload{[Super Viewdidload]; Fake data Mode 1 with dictionary _array = @[@{kheader: @ "12 Chai", kgirlsarr:@[@ "Lin Daiyu", @ "Xue Bao Chai", @ "Jia Yuanchun", @ "Jia Tanchun", @ "Shi Xiangyun", @ "Miaoyu", @ " Jia Chun ", @" Jia Spring ", @" Wang Xifeng ", @" Jia Chi ", @" Revan ", @" Qin keqing "], kfooter:@" Red Mansions "}, @{kheader: @" 12 Chai Vice Register "," kgirlsarr:@[@ "," Siborchen ", @" Erjie elder sister ", @" Jie ", @" 邢岫 smoke ", @" Li grain ", @" lowered ", @" Xia Jingui ", @" Autumn Tong ", @" Little Red ", @" age officer ", @" jiao apricot "], kfooter:@ "Red Mansions"}, @{kheader: @ "12 Chai and Deputy booklet", kgirlsarr:@[@ "Qing Wen", @ "hit people", @ "flat son", @ "Mandarin Duck", @ "purple cuckoo", @ "ying son", @ "Yu Yu", @ "Jin Yu", @ "Choi Wan", @ "chess", @ "Fang Guan", @ "Musk deer Moon", kfooter:@ "dream of Red Mansions"" } ]; Fake Data mode 2 with Class Package//style There are only two grouped plainuitableview *tableview = [[UITableView alloc]initwithframe:self.view.bounds Style:uitableviewstylegrouped]; Data source tableview.datasource = self; Added to Self.view [Self.view Addsubview:tableview];} Data source method, special case, important ~ How many groups do you have in common-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return _array.count;} Data source methods, each group, how many rows-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{// Returns the length of the corresponding dictionary in the array return [[_array[section] objectforkey:kgirlsarr] count];} The data source method, each row of each group should show how the interface (including the encapsulated data), the focus!!! Must be implemented otherwise, terminating app due to uncaught exception ' nsinternalinconsistencyexception ', Reason: ' UITableView dataSource Must return a cell from Tableview:cellforrowatindexpath: '-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static NSString *cellid = @ "Beyond"; UITableViewCell *cell = [TableView dequeuerEusablecellwithidentifier:cellid]; if (cell = = nil) {//Assuming that the pool is not taken, it generates 4 styles for a cell/* cell again: 1,default left-hand text 2,subtitle left the text large under the text small 3,value 1 left picture left text large right text small 3,value 2 disgusting left text small right text large */ cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid]; }//Set unique content in cell Cell.textLabel.text = [_array[indexpath.section] objectforkey:kgirlsarr][indexpath.row]; returns cell return cell; Data source method, what title is displayed at the beginning of the group-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (Nsinteger) section{ return [_array[section] objectforkey:kheader];} The data source method, the end of the group shows what title//-(NSString *) TableView: (UITableView *) TableView titleforfooterinsection: (Nsinteger) section// {//return [_array[section] objectforkey:kfooter];//} @end
Mode 2, replace the dictionary in the array with the class (model)
TwelveBeauties.h
twelvebeauties.h// 10_tableview//// Created by Beyond on 14-7-26.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import <foundation/foundation.h>//a member of the member array in the corresponding VIEWCONTROLLER.M- - > Dictionary @interface twelvebeauties:nsobject//UI controls with weak, string with copy, other objects with Strong@property (nonatomic,copy) NSString * header; @property (nonatomic,copy) nsstring *footer; @property (Nonatomic,strong) Nsarray *girls;//provides a class method, A construction method starting with the class name (return ID also) + (Twelvebeauties *) Twelvebeautieswithheader: (NSString *) header footer: (NSString *) Footer Girls :(Nsarray *) girls; @end
Twelvebeauties.m
twelvebeauties.m// 10_tableview//// Created by Beyond on 14-7-26.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import "TwelveBeauties.h" @implementation twelvebeauties//provides a class method, a construction method that begins with the class name ( Return ID also) + (Twelvebeauties *) Twelvebeautieswithheader: (NSString *) header footer: (NSString *) Footer girls: (Nsarray *) girls{ twelvebeauties *twelvebeauties = [[Twelvebeauties alloc]init]; Twelvebeauties.header = header; Twelvebeauties.footer = footer; Twelvebeauties.girls = girls; return twelvebeauties;} @end
Beyondviewcontroller.m
beyondviewcontroller.m//10_tableview////Created by Beyond on 14-7-25.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "BeyondViewController.h" #import "TwelveBeauties.h" @interface Beyondviewcontroller () <uitableviewdatasource>{//False data Nsarray *_array;} @end @implementation beyondviewcontroller-(void) viewdidload{[super viewdidload];//fake Data mode 1 with dictionary/* _array = @[@{khe Ader: @ "12", kgirlsarr:@[@ "Lin Daiyu", @ "Xue Bao Chai", @ "Jia Yuanchun", @ "Jia Tanchun", @ "Shi Xiangyun", @ "Miaoyu", @ "Jia Chun", @ "Jia Spring", @ "Wang Xifeng", @ "Jia Chi", @ "Revan", @ "Qin keqing", K footer:@ "Red Mansions"}, @{kheader: @ "12", kgirlsarr:@[@ "Incense Ling", @ "Siborchen", @ "Erjie elder sister", @ "Jie", @ "邢岫", @ "Li grain", @ "lowered", @ "Xia Jingui", @ "Autumn Tong", @ "Little Red", @ "age official", @ "jiao apricot", kfooter:@ "Red Mansions"}, @{kheader: @ "12 and Deputy Register", kgirlsarr:@[@ "Ching Wen", @ "hit people", @ "flat son", @ "Mandarin duck", @ "purple cuckoo", @ "ying Son" , @ "Yu Yu", @ "Jin Yu", @ "Choi Wan", @ "chess", @ "Fang Guan", @ "Musk deer Moon", kfooter:@ "Red Mansions"}];*///pseudo Data Mode 2 with class encapsulation after _array = @[ [Twelvebeauties twelvebeautieswithheader:@ "12-message" footer:@ "Red Mansions" girls:@[">@" Lin Daiyu ", @" Xue Bao Chai ", @" Jia Yuanchun ", @" JiaSpring ", @" Shi Xiangyun ", @" Miaoyu ", @" Jia Chun ", @" Jia Spring ", @" Wang Xifeng ", @" Jia Chi ", @" Revan ", @" Qin keqing "], [twelvebeauties twelvebeautieswithheader:@" 12-Chai Vice Register "footer:@" Red Mansions "girls:@[@", @ "Siborchen", @ "Erjie elder sister", @ "Jie", @ "邢岫", @ "Li grain", @ "lowered", @ "Xia Jingui" @ "Autumn Tong", @ "Little Red", @ "age officer", @ "jiao apricot"], [Tw Elvebeauties twelvebeautieswithheader:@ "12 and deputy Register" footer:@ "Red Mansions" girls:@[@ "Qing Wen", @ "hit people", @ "flat son", @ "Mandarin duck", @ "purple cuckoo", @ "ying son", @ "Yu Yu", @ "Jin Yu", @ "Choi Wan", @ "chess", @ "Fang Guan", @ "musk Deer Month"]]; Styles have only two grouped plainuitableview *tableview = [[UITableView alloc]initwithframe:self.view.bounds style: Uitableviewstylegrouped]; Data source tableview.datasource = self; Added to Self.view [Self.view Addsubview:tableview];} Data source method, special case, important ~ How many groups do you have in common-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return _array.count;} Data source methods, each group, how many rows-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{// Returns the length of the corresponding dictionary in the array//return [[_array[section] objectforkey:kgirlsarr] count]; After encapsulation with the data model twelvebeauties *twelvebeauties =_array[section]; return twelveBeauties.girls.count;} The data source method, each row of each group should show how the interface (including the encapsulated data), the focus!!! Must be implemented otherwise, terminating app due to uncaught exception ' nsinternalinconsistencyexception ', Reason: ' UITableView dataSource Must return a cell from Tableview:cellforrowatindexpath: '-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static NSString *cellid = @ "Beyond"; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellid]; if (cell = = nil) {//Assuming that the pool is not taken, it generates 4 styles for a cell/* cell again: 1,default left-hand text 2,subtitle left the text large under the text small 3,value 1 left picture left text large right text small 3,value 2 disgusting left text small right text large */ cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid]; }//Set unique contents in cell, the dictionary encapsulates false data//cell.textlabel.text = [_array[indexpath.section] Objectforkey:kgirlsarr][indexpath.row ]; Encapsulating the TWELVEB with a data modelEauties *twelvebeauties = _array[indexpath.section]; Cell.textLabel.text = [Twelvebeauties.girls objectAtIndex:indexPath.row]; returns cell return cell; Data source method, what title is displayed at the beginning of the group-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{// Dictionary encapsulation false Data//return [_array[section] objectforkey:kheader]; After encapsulation with data model twelvebeauties *twelvebeauties = _array[section]; return twelvebeauties.header;} Data source method, the title of the index of the group (the rightmost vertical bar of the Address book)-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{//with KVC \ KVO can be implemented in a single line of code return @[@ "register", @ "register", @ "deputy Register"];//Nsmutablearray *array = [Nsmutablearray array];//for (twelvebeauties *TB in _array) {//[array addobject:tb.header];//}//NSLog (@ "%@", array);//return array; } @end