UITableView

一   UITableView

//UITableView 继承于UIScrollView

//我们之后看到的所有能够自己滑动的视图的控件   全部都继承于UIScrollView的

UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];

 

//铺放数据源的代理设置 需要遵守UITableViewDataSource协议

tableView.dataSource = self;

//一些辅助性的代理方法  调整tableView样式  以及每一个cell的点击方法

tableView.delegate = self;

 

//设置没有分割线

tableView.SeparatorStyle = UITableViewCellSeparatorStyleNone;

 

//设置每一行的高度----通常情况下只在方法中来设立行高

tableView.rowHeight = 100;

//设置分区表头高度

tableView.sectionHeaderHeight = 100;

 

//设置分区表尾高度

tableView.sectionFooterHeight = 100;

 

 

#pragma mark --- 返回多少分区 默认是1---

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

 

 

#pragma mark --- 必须实现的 每个分区下 返回多少行 ---

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

{

    // 每个分区下 返回不同的行数

    /*

    if (section == 0)

    {

        return 20;

    }

    else if(section == 1)

    {

        return 9;

    }

    return 13;

     */

    

    return 20;

}

 

 

#pragma mark --- 返回cell的方法 必须实现的 ---

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

{

    

   // UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]autorelease];

    

 

    static NSString *cellIndentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];

    if (cell == nil)

    {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier];

    }

    

    // 设置主标题

    cell.textLabel.text = [NSString stringWithFormat:@"%ld分区", indexPath.section];

    

    // 设置右边的辅助视图

    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    

    // 设置副标题

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];

    

    

    // 设置点击渲染颜色

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    

    

    

    // 自定义点击渲染颜色

    // 前提条件:渲染颜色不能设置成 none

    /*

    UIView *myView = [[UIView alloc]init];

    myView.backgroundColor = [UIColor redColor];

    cell.selectedBackgroundView = myView;

    [myView release];

     */

    

    // 设置图片

    cell.imageView.image = [UIImage imageNamed:@"0.png"];

    

    // 辅助视图设置成自己的图片

    /*

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];

    imageView.image = [UIImage imageNamed:@"0.png"];

    cell.accessoryView = imageView;

    [imageView release];

    */

    return cell;

}

 

/*

#pragma mark --- 设置每行高度 ---

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.section == 0)

    {

        if (indexPath.row == 0)

        {

            return 200;

        }

        else

        {

            return 100;

        }

    }

    return 50;

}

 */

 

#pragma mark --- 点击cell的方法 ---

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"点击了 %ld分区 %ld", indexPath.section, indexPath.row);

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.backgroundColor = [UIColor redColor];

}

 

 

/*

#pragma mark --- 返回右边的标题 ---

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    return @[@"A", @"B", @"C"];

}

 

#pragma mark --- 返回每个分区的标题 ---

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

{

    if (section == 0)

    {

        return @"A";

    }

    else if (section == 1)

    {

        return @"B";

    }

    return @"C";

}

 */

 

 

/*

#pragma mark --- 分区表头高度 ---

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 50;

}

 

#pragma mark --- 自定义分区表头 ---

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    UIView *myView = [[[UIView alloc]init]autorelease];

    myView.backgroundColor = [UIColor redColor];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 30)];

    label.backgroundColor = [UIColor greenColor];

    label.text = [NSString stringWithFormat:@"%ld分区表头", section];

    [myView addSubview:label];

    [label release];

    return myView;

}

 

 

#pragma mark --- 设置分区表尾高度 ---

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

    return 50;

}

 

#pragma mark --- 自定义分区表尾 ---

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

{

    UIView *myView = [[[UIView alloc]init]autorelease];

    myView.backgroundColor = [UIColor redColor];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 30)];

    label.backgroundColor = [UIColor greenColor];

    label.text = [NSString stringWithFormat:@"%ld分区表尾", section];

    [myView addSubview:label];

    [label release];

    return myView;

}

 

posted @ 2016-02-23 20:54  mingxing  阅读(115)  评论(0编辑  收藏  举报