UITableView的基本使用(一)
2016-06-28 20:46
363 查看
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //设置tableView的数据源,通过数据源的协议方法可以设置tableView显示的内容。 _table.dataSource = self; //设置tableView的代理,通过代理方法可以得到tableView的一些操作响应和设置tableView的一些属性。 _table.delegate = self; //设置tableView的统一行高。 _table.rowHeight = 100; //设置tableView的统一区头高度 _table.sectionHeaderHeight = 30; } //设置每一行的行高 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return indexPath.row%2?50:100; } //plain样式的table区头始终在区的最上方,不会随着区向上移动,直到整个区被滑出table之外。 //grouped样式table的区头会随着区一起滑动。 //设置某个区的区头的标题 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [NSString stringWithFormat:@"第%lu区的区头",section]; } //设置某个区的区尾的标题 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return [NSString stringWithFormat:@"第%lu区的区尾",section]; } //设置tableView有几个区 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 5; } //设置tableView每个区的行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section == 0) { return 5; }else{ return 7; } } //tableView每一行显示的内容非常丰富,所以我们需要把显示的内容排列在一个单元格上,然后把单元格返回。 //设置tableView每一行的单元格 //在某一行的cell将要显示时调用 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //*cell的重用机制:每次需要显示一个cell时,先去tableView中寻找可以重用的cell,如果能够找到就直接使用,如果找不到,那么就创建一个新的cell。 //dequeueReusableCellWithIdentifier根据重用标示符在tableView中寻找一个可以重用的cell。如果能够找到那么返回这个cell,如果没有可以重用的cell,那么返回nil UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (!cell) { NSLog(@"调用了"); cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; //cell的autorelease必须写在if语句括号中,因为只有创建的cell才release,重用找到的cell不需要release [cell autorelease]; } //indexPath.row行号 //indexPath.section区号 cell.textLabel.text = [NSString stringWithFormat:@"第%ld区,第%ld行",indexPath.section,indexPath.row]; cell.imageView.image = [UIImage imageNamed:@"123.png"]; cell.detailTextLabel.text = @"细节"; cell.accessoryType = UITableViewCellAccessoryDetailButton; return cell; } //当tableView的某一行被点击时调用。 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ }
相关文章推荐
- serialVersionUID介绍与作用
- UITTableView滑动删除
- APUE环境配置
- UE4 Smart_Pointer_Library
- android quick settings中去掉不需要的图标
- 日常坑-Illegal connection port value '3306'
- 设计模式---Builder模式
- ueditor 简单/复杂工具栏切换
- UI布局--四种布局
- 【J2EE】org.hibernate.exception.SQLGrammarException: could not execute query错误
- build 时候把场景加进去的时候,场景有对应的序号
- 设置UITextField的内容
- NGUI 指定视口大小
- iOS开发 UITabBar角标 红点形式 (tabBarItem.badgeValue)
- burpsuite1.6安装使用方法详解
- String、StringBuffer、StringBuilder之间的区别
- UISearchBar 设置背景色,去掉黑线
- 使用手势对UIImageView进行缩放、旋转和移动
- UGUI代码添加带参数的Button事件
- PostgreSQL:“ FATAL: requested WAL segment00800002A0 has already been removed”