码迷,mamicode.com
首页 > 其他好文 > 详细

OC UTableView

时间:2015-11-13 19:33:00      阅读:409      评论:0      收藏:0      [点我收藏+]

标签:


UITableView 遵守的两个协议:


UITableViewDataSource

UITableViewDelegate

#import "ViewController.h"


@interface ViewController ()

@property (nonatomic,strong) NSArray *array;

@property (nonatomic,strong) NSArray *arrayI;

@end


@implementation ViewController


- (void)viewDidLoad {

    [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


OC UTableView

标签:

原文地址:http://my.oschina.net/u/2483162/blog/530094

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!