已经有将近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; //设置每行的高度(也可以通过不同分区设置不同行高)
}
// 第几区的头
-(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;
}
//每个Cell的点击事件代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//这个方法中可以写出cell的点击事件实现,是tableView操作协议的代理方法中十分重要的一个代理方法,也是十分的常用,希望熟练掌握
}
注:鉴于刚刚回归CSDN,如果有想知道的知识,如果我可以解答的我会写在博客中,如果我不懂的大家可以相互学习与讨论,觉得我哪里写的不好的地方,希望可以提出,求知若渴,成功往往是在挫折中成长起来的