UITableView 多個分區的展示

#import "TableViewController.h"

@interface TableViewController ()
@property (nonatomic,strong) NSArray *sectionArray;//分區標題
@property (nonatomic,strong) NSArray *AsiaArray;//亞洲
@property (nonatomic,strong) NSArray *EuropeanArray;//歐洲
@property (nonatomic,strong) NSArray *AmericasArray;//美洲
@property (nonatomic,strong) NSArray *dataArray;//數據源
@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _sectionArray = @[@" 亞洲",@" 歐洲",@" 美洲"];
    _AsiaArray = @[@"中國",@"韓國",@"日本",@"泰國",@"越南"];
    _EuropeanArray = @[@"德國",@"英國",@"法國",@"意大利"];
    _AmericasArray = @[@"美國",@"加拿大"];
    _dataArray = @[_AsiaArray,_EuropeanArray,_AmericasArray];
}


#pragma mark - Table view data source

//自定義區頭
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *label= [[UILabel alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 30)];
    label.font = [UIFont boldSystemFontOfSize:14];
    //分區的標題
    label.text = self.sectionArray[section];
    return  label;
}
//區頭高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

//分區個數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   
    return _sectionArray.count;
}

//每個分區cell的個數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
  (NSInteger)section {
   
    
    return  [_dataArray [section]count];
}

//cell的重用機制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //cell顯示的內容
    cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
    
    return cell;
}


@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章