1.dataSource:数据代理对象
2.delegate:普通代理对象
3.numberOfSectionslnTableView:获得组数协议
4.numberOfrowslnSection:获取行数协议
5.cellForRowAtindexPath:创建单元格协议
首先 数据视图是啥?
苹果手机 点击设置出来的一系列选项 可以上下滚动选择
ViewController.h文件的使用
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<
//实现数据视图的普通协议
//数据视图的普通时间处理
UITableViewDelegate,
//实现数据视图的数据代理协议
//处理数据视图的数据代理
UITableViewDataSource
>
{
//定义一个数据视图对象
//数据视图是用来显示大量相同格式的大量信息的视图
//例如:电话通讯录,QQ好友,微信朋友圈
UITableView * _tableView;
}
@end
ViewController.m文件的实现:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建数据视图
//P1:数据视图的位置
//p2:数据视图的风格 UITableViewStylePlain 普通风格
//UITableViewStyleGrouped:分组风格
//self.view.bounds:跟当前的视图(整个屏幕)一样大
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
//设置数据视图的代理
_tableView.delegate = self;
//设置数据视图的数据源对象
_tableView.dataSource = self;
[self.view addSubview:_tableView];
[_tableView release];
//分割线颜色
_tableView.separatorColor = [UIColor redColor];
//分割线类型
//_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//行高
_tableView.rowHeight = 100;
//背景颜色
_tableView.backgroundColor = [UIColor orangeColor];
//_tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"2_4.jpg"]];
//背景图片
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
imageView.image = [UIImage imageNamed:@"2_4.jpg"];
_tableView.backgroundView = imageView;
[imageView release];
}
//获取每组元素的个数(行数)
//必须要实现的协议函数
//程序在显示数据视图时会调用此函数 返回值表示每组元素的个数
//p1:数据视图对象本身 p2:哪一组需要的函数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
//设置数据视图的组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
//创建单元格对象函数
//indexPath:索引 组数 行数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellStr = @"cell";
UITableViewCell * cell = [_tableView dequeueReusableCellWithIdentifier:cellStr];
if ( cell == nil ){
//创建一个单元格对象
//p1:单元格的样式 p2:单元格的复用标记
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
}
NSString * str = [NSString stringWithFormat:@"第%ld组,第%ld行",indexPath.section,indexPath.row];
//cell选中效果
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//将单元格的主文字内容赋值
cell.textLabel.text = str;
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end