Eventually:
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 Formal List", 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 Book, "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 book", 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 only two grouped plainuitableview *tableview = [[UITableView alloc]initwithframe:self.view.bounds Style:uitableviewstylegrouped]; Data source tableview.datasource = self; Add to Self.view [Self.view Addsubview:tableview];} Data source methods, exceptions, important ~ How many groups-(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) {//If not in the pool, regenerate one cell/* Cell 4 styles: 1,default left-hand Figure 2 , subtitle left the text large under the text small 3,value 1 left figure 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>//corresponds to a member of the member array in the 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 Chai Formal List", kgirlsarr:@[@ "Lin Daiyu", @ "Xue Bao Chai", @ "Jia Yuanchun", @ "Jia Tanchun", @ "Shi Xiangyun", @ "Miaoyu", @ "Jia Chun", @ "Jia", @ "Wang Xifeng", @ "Jia Chi", @ "Revan", @ "Qin keqing", K footer:@ "Red Mansions"}, @{kheader: @ "12-chai", kgirlsarr:@[@ "Xiang Ling", @ "Siborchen", @ "Erjie elder sister", @ "Jie", @ "邢岫", @ "Li grain", @ "lowered", @ "Xia Jingui", @ "Autumn Tong", @ "Little Red", @ "age official", @ "jiao apricot", kfooter:@ "Red Mansions"}, @{kheader: @ "12 Chai and Deputy book", 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 Chai Formal List" 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 Book "footer:@" Red Mansions "girls:@[@", @ "Siborchen", @ "Erjie elder sister", @ "Jie", @ "邢岫", @ "lowered", @ "Xia Jingui", @ "Red", @ "Autumn Tong", @ "Xiao Hong", @ "age officer", @ "jiao apricot"], [Tw Elvebeauties twelvebeautieswithheader:@ "12 and deputy book" 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"]]; There are only two styles of grouped plainuitableview *tableview = [[UITableView alloc]initwithframe:self.view.bounds style: Uitableviewstylegrouped]; Data source tableview.datasource = self; Add to Self.view [Self.view Addsubview:tableview];} Data source methods, exceptions, important ~ How many groups-(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) {//If not in the pool, regenerate one cell/* Cell 4 styles: 1,default left-hand Figure 2 , subtitle left the text large under the text small 3,value 1 left figure 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 Twelvebeaut with a data modelIES *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 one line of code return @[@ "Formal list", @ "sub-book", @ "sub-book"];//Nsmutablearray *array = [Nsmutablearray array];//for (twelvebeauties *TB in _array) {//[array addobject:tb.header];//}//NSLog (@ "%@", array);//return array; } @end