UITableView:代碼atom
section:組別url
row:行號spa
【1】拖入一個UITableViewcode
【2】將TableView的dataSource與控制器鏈接blog
【3】首先得遵循UITableView的數據源協議<UITableViewDataSource>ci
代碼get
1.加入顯示數據內容it
- (void)viewDidLoad { [super viewDidLoad]; self.gd=@[@"廣州" ,@"梅州",@"深圳"]; self.hn=@[@"長沙",@"益陽"]; }
2.設置總共有多少個組(如上圖一共2個組)io
#pragma mark -數據源方法 #pragma mark 下面的方法返回的是一共有多少組數據 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2;// 返回2組數據 }
3.設置一個組分別有多少個成員(多少行)table
#pragma mark 第section組裏面有多少行數據 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 返回一個組裏面有多少行 // 將廣東放在前面 if(section==0) {// 廣東 return self.gd.count; }else {// 湖南 return self.hn.count; } }
4.顯示每一行具體數據內容,要獲得2個組的內容,須要進行property
@interface ViewController () @property (nonatomic,strong)NSArray *gd; @property (nonatomic,strong)NSArray *hn; @end
#pragma mark 返回每一行顯示的具體數據 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; NSString *city=nil; // 廣東 if(indexPath.section==0) { city=self.gd[indexPath.row]; }else { city=self.hn[indexPath.row]; } // 獲得內部的label 設置cell上面顯示的文本數據 cell.textLabel.text=city; // 傳出一個行號 return cell; }
5.設置組的標題
#pragma mark 返回第section組的標題 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return section==0?@"廣東":@"湖南"; }
6.設置組的尾標題
#pragma mark 返回第section組的尾標題
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { return section==0?@"廣東不少帥哥":@"湖南不少美女"; }
7.其餘
經過設置TableView的Style 屬性,進行對顯示效果的分組樣式進行修改。