iOS基础--UITableView,UITableViewController

这篇博客主要介绍了iOS开发中常见的UITableView及其UITableViewDataSource协议。内容涵盖tableView的表单结构、两种代理方法(UITableViewDelegate和UITableViewDataSource)、常用的数据源方法如分区数、行数的设置,以及cell的创建、高度设定、头尾视图定制和点击事件处理。适合初学者了解和掌握UITableView的基础用法。
摘要由CSDN通过智能技术生成

已经有将近1年时间没有在CSDN上写博客了,现在准备回归CSDN,鉴于一开始讲述的都是iOS开发的一些基础知识,所以一年后的回归一开始还是会写一些基础方面的知识,希望对于刚接触iOS没多久的童鞋们有所帮助!

进入正题:

@UITableView:

iOS开发中最常用的基础页面,其形式与结构就是所谓的表单,分成多栏的结构,其中每一栏被称之为UITableViewCell,简称cell.

@tableView的两种协议:UITableViewDelegate UITableViewDataSource

这两种代理方法分别是tableView的数据协议与操作协议,从字面上可以看出,delegate是操作协议,dataSource是数据协议.每当创建一个tableView对象的时候,都要遵守这两种协议,不对tableView上进行任何操作的时候可以不遵守UITableViewDelegate协议.

@下面介绍一下UITableViewDataSource协议中常用的代理方法

创建tableView视图的时候,一定需要分区数,行数,和显示:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 2; //分区数设置(当前为2个分区)(必须实现)

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

{

if (section==0) {

return 1;

} else {

return 2;

}

} //行数设置(第一个分区行数1,第二个分区行数2)(必须实现)

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

{

//定义cell的唯一标识

  static NSString * cellIdentifier = @"cell";

    //tableView通过cell的标识,NSSet(重用队列)中获取cell对象

  UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    //判断重用队列中存不存在cell标识的对象

    if (!cell) {

        //如果不存在,创建cell对象

        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 

reuseIdentifier:cellIdentifier]autorelease];

//注:ARC环境下不需要加autorelease,MRC下需要

            }

    return cell;

} //设置每行要显示的内容(必须实现)


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

{

return 40; //设置每行的高度(也可以通过不同分区设置不同行高)

}


下面是非必须实现且比较常用的tableView的数据代理方法:

// 第几区的头

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

{

    return [NSString stringWithFormat:@"%ld",section];

}

//第几区的尾

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

{

    return [NSString stringWithFormat:@"%ld区的尾",section];

}


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

{

    return @[@"0",@"1",@"2",@"3"];

}


//头的视图

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

{

    UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 10)];

    view1.backgroundColor = [UIColor yellowColor];

    return view1;

}

//尾的视图

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

{

    UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 10)];

    view2.backgroundColor = [UIColor blueColor];

    return view2;  

}

//头的高度(分区)

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

{

    return 50;

}

//尾的高度(分区)

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

{

    return 50;

}


@下面介绍一下 UITableViewDelegate协议中最常用的代理方法

//每个Cell的点击事件代理方法

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

{

  //这个方法中可以写出cell的点击事件实现,是tableView操作协议的代理方法中十分重要的一个代理方法,也是十分的常用,希望熟练掌握

}


@tableView介绍完之后,就可以简单介绍一下UITableView的控制器UITableViewController,UITableViewController是对于tableView视图的操作控制器,内部封装了对于tableView的协议与代理方法,继承之后可以在任意视图控制器下直接使用,十分方便,降低了tableView的使用成本.

@iOS开发中,熟练掌握这些基础性的东西是必须的,希望会对于还在学习基础的童鞋会有一些帮助.

注:鉴于刚刚回归CSDN,如果有想知道的知识,如果我可以解答的我会写在博客中,如果我不懂的大家可以相互学习与讨论,觉得我哪里写的不好的地方,希望可以提出,求知若渴,成功往往是在挫折中成长起来的



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值