OC UTableView


UITableView 遵照的兩個協議:atom


UITableViewDataSource
spa

UITableViewDelegate.net

#import "ViewController.h"
3d


@interface ViewController ()代理

@property (nonatomic,strong) NSArray *array;orm

@property (nonatomic,strong) NSArray *arrayI;對象

@end get


@implementation ViewControllerit


- (void)viewDidLoad {io

    [super viewDidLoad];

    

    //實例化一個UITableView對象

    UITableView *tableView = [[UITableView alloc]initWithFrame:   [[UIScreen mainScreen]bounds]style:UITableViewStyleGrouped];

   // [[UIScreen mainScreen]bounds]  主屏的尺寸

   // 樣式有兩種 UITableViewStyleGrouped(分組)  UITableViewStylePlain(不分組)

    

    //設置代理和數據源

    tableView.delegate = self;

    tableView.dataSource = self;

    

    //tableview設置行高

    tableView.rowHeight = 50;

    

    _array = @[@"歌曲一",@"歌曲二",@"歌曲三",@"歌曲四"];

    _arrayI = @[@"一次就好",@"喜歡你",@"暖暖",@"之前之後"];

    [self.view addSubview:tableView];

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


//UITableViewDataSource 協議必需要寫的方法一  用來設置列表每組的行數

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    //第二組返回三行 section也是從第0行開始

    if (section == 2) {

        return 3;

    }

    return _array.count;

}


//UITableViewDataSource 協議可選擇的方法一  用來設置返回多少組

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 4;

}


//UITableViewDataSource 協議可選擇的方法二  用來設置頭部標題

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    return @"頭部標題";

}


//UITableViewDataSource 協議可選擇的方法三  用來設置尾部標題

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

{

    

    return @"尾部標題";

}




//UITableViewDataSource 協議必需要寫的方法二  用來設置tableView中每一個cell的內容

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    // UITableViewCell 單元格複用

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

    }

    //cell設置內容

    cell.textLabel.text = _array[indexPath.row];

    

    //給不一樣組設置內容

    if (indexPath.section == 1) {

        cell.textLabel.text = _arrayI[indexPath.row];

    }

    

    

    //UITableViewCell的四種樣式:

    //1.UITableViewCellStyleDefault

//    2.UITableViewCellStyleSubtitle

//    3.UITableViewCellStyleValue1

//    4.UITableViewCellStyleValue2

    

    

       cell.detailTextLabel.text = @"hello";

    return cell;

}

@end

相關文章
相關標籤/搜索