iOS入门(三十三) UITableView表视图

本文介绍UITableView的基本配置方法,包括创建TableView、设置样式、定义代理及数据源等,并演示如何为TableView添加分区标题、头部和底部视图。
 UITableView表视图  
不允许编辑,只用于显示用Lable
只能上下滚  
表视图继承于UIScrollView
类型可选:没有或者单线

 UITableViewCellSeparatorStyleSingleLineEtched  Grouped类型使用

DataSouce数据源  负责给tableView提供数据

表视图的配置

NSIndexPath

两个属性:section(分区)  row(行数)


 

@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //1、建立TableView

    UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(10, 20, 300, 450) style:UITableViewStyleGrouped];

    tableview.backgroundColor = [UIColor colorWithRed:0.5 green:0 blue:0.8 alpha:0.6];

    [self.view addSubview:tableview];

    [tableview release];

    tableview.separatorStyle UITableViewCellSeparatorStyleSingleLine;

    tableview.separatorColor = [UIColor blackColor];

    tableview.rowHeight 150;

    //2、设置TableView的两个代理

    //让代理人响应TableView的一些状态(点击事件/视图操作)

    tableview.delegate self;

    //让代理人(ViewController)给TableView提供数据(多少行,每行有哪些内容)

    tableview.dataSource self;

    

}

//给分区顶部设置一个视图

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

{

    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];

    view.backgroundColor = [UIColor greenColor];

    return view;

}

//给分区底部设置一个视图

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

{

    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];

    view.backgroundColor = [UIColor blueColor];

    return view;

}

#pragma mark - tableView的delegate协议方法

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

{

    NSLog(@"%@",indexPath);

}

#pragma mark - tableView的dataSouce协议方法

//给每一个分区设置标题

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

{

    if (section == 0) {

        return @"付莉莉";

    }

    return @"分区“";

}

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

{

    return @[@"尚需",@"姜维东",@"马晓庆"];

}

//分区的数量

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 5;

}

//每一行的行高

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

{

    return 50;

}

//3、代理人实现协议中要求的方法

//设置TableView的行数

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

{

    return 20;

}

//每行的cell显示

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

{

    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell~"];

    //中间的标签视图

    cell.textLabel.text = [NSString stringWithFormat:@"section:%d   row:%d",indexPath.section,indexPath.row];

    //左侧的图片视图

    cell.imageView.image = [UIImage imageNamed:@"76.jpg"];

    //右侧的附属视图

    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

//    cell setAccessoryView:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值