Create a new module (class), similar to JavaBean
// // Table control // // Created by Yaguangzhu on 15/8/13. // Copyright (c) Yaguangzhu 2015. All rights reserved. // #import <foundation/foundation.h>@ Interface Hmstudent:nsobject@property (nonatomic,copy) nsstring *title; @property (nonatomic,copy) nsstring *DESC; @property (Nonatomic,strong) Nsarray *students; @end
////VIEWCONTROLLER.M//Table Control////Created by Yaguangzhu on 15/8/13.//Copyright (c) 2015 Yaguangzhu. All rights reserved.//#import "ViewController.h"#import "HMStudent.h"@interfaceViewcontroller () <UITableViewDataSource>@property (Weak, nonatomic) Iboutlet UITableView*TableView, @property (strong,nonatomic) Nsarray*dataList;@end@implementationViewcontroller-(Nsarray *) datalist{if(_datalist = =Nil) {hmstudent*STU1 =[[Hmstudent alloc]init]; Stu1.title=@"Black Horse 1 gas"; Stu1.desc=@"Good"; Nsmutablearray*array1 =[Nsmutablearray array]; for(intI=0; i<Ten; i++) {[Array1 addobject:[nsstring stringWithFormat:@"%@--%04d", Stu1.title,i]]; Stu1.students=array1; } hmstudent*STU2 =[[Hmstudent alloc]init]; Stu2.title=@"Black Horse 2 gas"; Stu2.desc=@"Good"; Nsmutablearray*array2 =[Nsmutablearray array]; for(intI=0; i<Ten; i++) {[Array2 addobject:[nsstring stringWithFormat:@"%@--%04d", Stu2.title,i]]; Stu2.students=array2; _datalist=@[stu1,stu2]; } } return_datalist;}-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{returnSelf.dataList.count;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{hmstudent*stu =Self.datalist[section]; returnStu.students.count;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{UITableViewCell*cell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:nil]; Hmstudent*stu =Self.datalist[indexpath.section]; Cell.textLabel.text=Stu.students[indexpath.row]; returncell;}//-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView; //Default is 1 if not implemented-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{hmstudent*stu =Self.datalist[section]; returnStu.title;}//fixed font style. Use Custom view (UILabel) If you want something different-(NSString *) TableView: (UITableView *) TableView titleforfooterinsection: (nsinteger) section{hmstudent*stu =Self.datalist[section]; returnStu.desc;}- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
Ios-uitableview Multi-group tables