UITableView
2015-08-07 18:40
459 查看
UITableView
// 让数组里的元素个数和行数保持相同
// return self.arr.count;
// 奇数分区有5行,偶数分区有10行
// 先执行设置分区的方法,后执行每个分区有多少行
if (section%2==0) {
return 10;
}else{
return 5;
}
}
// 创建cell
// static特点
// 1.只初始化一次
// 2.如果没有初始值,默认是0
// 3.直到程序结束,才会消失
// 当cell显示结束后,会把cell统一的放到重用池中,等需要cell显示了,先从重用池中先找,看有没有闲置的的cell,如果有的话就用闲置的cell,如果没有在创建
// cell的重用目的就是为了节约创建成本,用有限的cell把所有数据都显示出来
// 给重用池先设置一个重用的标志,根据这个标志可以找到对应的重用池
static NSString *reuse=@”reuse”;
// tableView通过重用标志在重用池中寻找cell,如果有闲置的cell,cell会保存一个有效的cell对象地址,如果没有,cell里面则是nil,空
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
// 如果没有cell则创建cell
if (!cell) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
}
// 对cell进行赋值
// cell里有默认的三个控件
cell.textLabel.text=self.arr[indexPath.row];
cell.detailTextLabel.text=[NSString stringWithFormat:@”%ld”,indexPath.section];
cell.imageView.image=[UIImage imageNamed:@”11.jpg”];
// indexPath.row保存的是行数,从0开始
// NSLog(@”%ld”,indexPath.row);
return cell;
}
// 1.dataSource协议
return 6;
}
return @”网易新闻”;
}
return @”发送到发送到”;
}
return @[@”1”,@”2”,@”3”,@”4”];
}
// 2.delegate协议
NSLog(@”section:%ld row:%ld”,indexPath.section,indexPath.row);
// 打印当前电机的人名叫什么
NSLog(@”%@”,self.arr[indexPath.row]);
SecondViewController *secVC=[[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
}
import “MainViewController.h”
import “SecondViewController.h”
@interface MainViewController ()pragma mark tableView第一个必须实现的协议方法,指定分区内有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{// 让数组里的元素个数和行数保持相同
// return self.arr.count;
// 奇数分区有5行,偶数分区有10行
// 先执行设置分区的方法,后执行每个分区有多少行
if (section%2==0) {
return 10;
}else{
return 5;
}
}
pragma mark 第二个协议方法,主要是用来显示数据
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{// 创建cell
// static特点
// 1.只初始化一次
// 2.如果没有初始值,默认是0
// 3.直到程序结束,才会消失
// 当cell显示结束后,会把cell统一的放到重用池中,等需要cell显示了,先从重用池中先找,看有没有闲置的的cell,如果有的话就用闲置的cell,如果没有在创建
// cell的重用目的就是为了节约创建成本,用有限的cell把所有数据都显示出来
// 给重用池先设置一个重用的标志,根据这个标志可以找到对应的重用池
static NSString *reuse=@”reuse”;
// tableView通过重用标志在重用池中寻找cell,如果有闲置的cell,cell会保存一个有效的cell对象地址,如果没有,cell里面则是nil,空
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
// 如果没有cell则创建cell
if (!cell) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
}
// 对cell进行赋值
// cell里有默认的三个控件
cell.textLabel.text=self.arr[indexPath.row];
cell.detailTextLabel.text=[NSString stringWithFormat:@”%ld”,indexPath.section];
cell.imageView.image=[UIImage imageNamed:@”11.jpg”];
// indexPath.row保存的是行数,从0开始
// NSLog(@”%ld”,indexPath.row);
return cell;
}
// 1.dataSource协议
pragma mark 设置分区个数
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return 6;
}
pragma mark 分区头标题
(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section{return @”网易新闻”;
}
pragma mark 分区尾标题
-(NSString )tableView:(UITableView )tableView titleForFooterInSection:(NSInteger)section{return @”发送到发送到”;
}
pragma mark 索引
(NSArray )sectionIndexTitlesForTableView:(UITableView )tableView{return @[@”1”,@”2”,@”3”,@”4”];
}
// 2.delegate协议
pragma mark tableView的点击方法
(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath{NSLog(@”section:%ld row:%ld”,indexPath.section,indexPath.row);
// 打印当前电机的人名叫什么
NSLog(@”%@”,self.arr[indexPath.row]);
SecondViewController *secVC=[[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
}
相关文章推荐
- RXSwift 关于tableview的创建(一)
- UITableView进阶:在编辑状态时修改cell
- 分离 UITableView 的 Delegate 和 Datasource
- iOS-74-解决UITableViewStyleGrouped 各个section间距调整问题
- ios Tableview行高的计算
- Swift ☞ UITableView(一)
- 在iOS中怎样创建可展开的Table View?(下)
- UITableView官方文档翻译
- 带分区的UITableView的创建(省市区字典)
- iOS解决键盘遮挡输入框问题
- iOS tableview距离顶部有一段空白
- 刷新某一行或某一段
- tableView简单认识以及性能优化
- storyboard 让TableViewCell的分割线顶到最左边
- iOS学习笔记之UI-UISearchController-And-UITableView
- UITableView
- "Auto Layout still required after executing -layoutSubviews” iOS7上崩溃sdk缺陷
- quick-3.5 打包加密的个人总结
- [IOS]今天开始学UI---UITextField&&UITextView
- 报错:require_once cannot allocate memory----php,以前自己弄的稍微有点特殊的开发环境