UI之tableView的两个协议UITableViewDataSource、UITableViewDelegate
来源:互联网 发布:google网站数据统计 编辑:程序博客网 时间:2024/05/16 16:03
创建一个MangoViewController在其.h文件中引入UITableView数据源协议,代理协议(多个协议之间用' , '隔开)
@interface MangoViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
接下来,在其.m文件中写入代理和数据源的应用代码:
self.view.backgroundColor = [UIColor orangeColor];
//设置导航栏标题
self.navigationItem.title = @"笑花";
//创建一个UITableView
self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
//分割线颜色
self.tableView.separatorColor = [UIColor blueColor];
//分割线样式,默认是SingleLine样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//行高
self.tableView.rowHeight = 88.0;
//背景颜色
self.tableView.backgroundColor = [UIColor magentaColor];
//设置tableView数据源代理
self.tableView.dataSource = self;
self.tableView.delegate = self;
//添加到当前视图上
[self.view addSubview:self.tableView];
[self.tableView release];
}
#pragma mark -------UITableViewDataSource(为了区分和方便理解,这里引入了标签#prama mark------)
//返回分区中有多少行,系统默认tableView有一个分区
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 6;
}
//将要显示的cell在哪个位置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//重用机制实现原理
//重用标示符 重用队列,出屏幕会被添加到重用队列,cell将要显示的时候先从重用队列里去取,如果重用队列是空的,就创建一个新的cell
//定义一个重用标示符
//static,静态区局变量,只需要创建一次,程序退出时释放
static NSString *cellIdentifier = @"Cyclecell";
//去tableView重用队列里取重用标识符为“Cyclecell”的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//cell == nil 当cell为空时,也就是重用队列里没有可用的cell,那么就创建一个新的cell
if (cell == nil) {
//切记,这里不能重新创建,而是继续使用上面创建好的cell
//重用队列里没有可用的cell,重建一个,然后把新建的cell的重用标识符设置为上边定义的重用标示符
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
//insert code····
//cell相关属性设置和赋值都可以在这里进行
//设置背景颜色
cell.backgroundColor = [UIColor magentaColor];
//设置cell标题
cell.textLabel.text = @"Winner";
//里面有两个属性,一个是section区域,一个是row行
// NSLog(@"%lu = %p",indexPath.row,&cell);
if (indexPath.row == 0) {
cell.textLabel.text = @"琪琪";
}else if (indexPath.row == 1){
cell.textLabel.text = @"俊俊";
}else if (indexPath.row == 2){
cell.textLabel.text = @"茹茹";
}else if (indexPath.row == 3){
cell.textLabel.text = @"娟娟";
}else if (indexPath.row == 4){
cell.textLabel.text = @"豆豆";
}else if (indexPath.row == 5){
cell.textLabel.text = @"呆呆";
}
//设置副标题
// cell.detailTextLabel.text = @"赢家";
if (indexPath.row == 0) {
cell.detailTextLabel.text = @"星";
}else if (indexPath.row == 1){
cell.detailTextLabel.text = @"星星";
}else if (indexPath.row == 2){
cell.detailTextLabel.text = @"小星星";
}else if (indexPath.row == 3){
cell.detailTextLabel.text = @"星";
}else if (indexPath.row == 4){
cell.detailTextLabel.text = @"星星";
}else if (indexPath.row == 5){
cell.detailTextLabel.text = @"小星星";
}
//设置图片
cell.imageView.image = [UIImage imageNamed:@"1.png"];
//返回创建好的cell
return cell;
}
//在TableView里有多少个分区,默认是一个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
//返回每个区头的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"窗寒西岭千秋雪";
}else if (section == 1){
return @"千山鸟飞绝";
}
return @"赢家";
}
//返回每个区尾的标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if (section == 0) {
return @"门泊东吴万里船";
}else if (section == 1){
return @"万径人踪灭";
}
return @"输家";
}
//返回右侧索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSArray *titleArray = @[@"音乐",@"舞蹈",@"乐器"];
return titleArray;
}
#pragma mark ------UITableViewDelegate(标签)
//设置区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 44;
}
//设置区尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 44;
}
//自定义分区的头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *blueV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
blueV.backgroundColor = [UIColor yellowColor];
UILabel *label = [[UILabel alloc]initWithFrame:blueV.frame];
label.text = @"你是谁啊";
label.textAlignment = NSTextAlignmentCenter;
[blueV addSubview:label];
return blueV;
}
//自定义分区的尾部
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *blueV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
blueV.backgroundColor = [UIColor cyanColor];
UILabel *label = [[UILabel alloc]initWithFrame:blueV.frame];
label.text = @"傻瓜,你不认识我了么";
label.textAlignment = NSTextAlignmentCenter;
[blueV addSubview:label];
return blueV;
}
//cell的选中方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
NSLog(@"****");//选中区域时控制台的输出结果
}else if (indexPath.section == 1){
NSLog(@"@@@@");
}else if (indexPath.section == 2){
NSLog(@"####");
}
NSLog(@"O,No");
}
//返回每一行的高度,rowHeight只能统一设置高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
if (indexPath.row == 0) {
return 60;//设置第一分区第一行的高度为60,其他为44
}
}else if (indexPath.section == 1){
if (indexPath.row == 0) {//设置第二分区第一行的高度为100,其他为44
return 100;
}
}
return 44;
}
- UI之tableView的两个协议UITableViewDataSource、UITableViewDelegate
- UITableViewDataSource 和 UITableViewDelegate协议中常用方法
- UITableViewDataSource, UITableViewDelegate
- iOS ,UITableViewDataSource 和 UITableViewDelegate协议中常用方法
- iOS ,UITableViewDataSource 和 UITableViewDelegate协议中常用方法
- UITableViewDataSource和UITableViewDelegate内常用的13个方法
- iOS UITableViewDelegate UITableViewDataSource的含义 和执行顺序
- iOS UITableViewDelegate UITableViewDataSource 执行顺序
- 怎样创建表格---UITableView UITableViewDataSource UITableViewDelegate
- 怎样创建表格---UITableView UITableViewDataSource UITableViewDelegate .
- UITableViewDataSource和UITableViewDelegate函数(一)
- 如何将UITableViewDelegate和UITableViewDataSource从UITableViewController中分离出来?
- 如何将UITableViewDelegate和UITableViewDataSource从UITableViewController中分离出来?
- 在UITableView中iOS UITableViewDelegate UITableViewDataSource执行顺序
- UI tableView 的优化
- UITableViewDelegate的代理方法
- UITableViewDelegate
- UITableViewDelegate
- 在Windows下安装Hadoop
- Linux负载均衡环境搭建-3 配置nginx为负载均衡前置
- 设计模式之静态代理设计模式
- segue使用及区别点
- ALV GRID 响应回车事件修改内容
- UI之tableView的两个协议UITableViewDataSource、UITableViewDelegate
- JS中的双向数据绑定及Object.defineProperty方法
- 1029. Median (25)
- 获知局域网内他人的上网数据和行为
- IOS NSString 装 NSData两种方式的不同
- Javascript面向对象编程(二):构造函数的继承
- 用Button子类化封装假的分段控件
- 测试相关,上线相关 经验总结odps sqltask(自己看的)
- 少量修改