UITableView
2016-06-20 23:08
323 查看
在IOS开发中,最常用的UI控件,应该就是UITableView了,在这里简单的对其进行介绍.
UITableView有数据源方法和代理方法,数据源方法是将模型数据显示到视图上,而代理方法是对TableView进行操作.
初始化方法:
下面分别介绍UITableView的数据源方法和代理方法:
数据源方法:
代理方法:
补充:
UITableView有数据源方法和代理方法,数据源方法是将模型数据显示到视图上,而代理方法是对TableView进行操作.
初始化方法:
//UITableViewStylePlain 平板格式 //UITableViewStyleGrouped 分组格式 UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; //常用属性 //设置表格可编辑 tableView.editing = YES; //设置tableView自带的分割线 tableView.separatorStyle = UITableViewCellSeparatorStyleNone; //设置tableView的行高(全部行) tableView.rowHeight = 50;
下面分别介绍UITableView的数据源方法和代理方法:
数据源方法:
//成为自己的数据源 tableView.dataSource = self; #pragma mark - UITableViewDataSource 数据源方法 /** 每个分组中的行数(必须实现) */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.foods.count; } /** 每一行显示的内容(必须实现) */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"food"; //取缓冲池查找可重用的单元格 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { //UITableViewCellStyleDefault, 默认类型, 标题 + 图像 //UITableViewCellStyleValue1, 标题 + 明细 + 图像 //UITableViewCellStyleValue2, 不显示图像 //UITableViewCellStyleSubtitle 标题 + 明细 + 图像 //实例化新的单元格 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.textLabel.text = self.foods[indexPath.row]; return cell; } //下面的方法都是可选的方法 ///** 设置表格的组数 */ //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ // return 2; //} /** 设置组标题 */ - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return @"Header"; } /** 设置组尾标 */ - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return @"Footer"; }
代理方法:
//成为自己的代理 tableView.delegate = self; #pragma mark - UITableViewDelegate 代理方法 /** 选中哪行 */ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"%@", self.foods[indexPath.row]); } /** 删除行 */ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) {//删除 //从数组中删除 [self.foods removeObjectAtIndex:indexPath.row]; //表格局部刷新 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } else if(UITableViewCellEditingStyleInsert){//插入 [self.foods addObject:@"满汉全席"]; [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } } /** 返回编辑的类型 (默认是UITableViewCellEditingStyleDelete)*/ - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ //这个返回值,就使得TableView可选中(用于多选,批量操作) return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; } /** * 表格的移动 */ - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ // 1.从源数据中取出 NSString *footName = self.foods[sourceIndexPath.row]; // 2.然后删除 [self.foods removeObjectAtIndex:sourceIndexPath.row]; // 3.再插入到数组的目标位置 [self.foods insertObject:footName atIndex:destinationIndexPath.row]; // 4.刷新表格 [tableView reloadData]; } /** * 设置tableView的行高(每行可以单独设置) */ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row % 2){ return 50; }else{ return 80; } }
补充:
// 数据多,应该往上滚动 NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:self.foods.count - 1 inSection:0]; [self.tableView scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES]; /** * 添加索引 */ - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return nil; }
相关文章推荐
- android UI TextView setText显示服务器返回数据
- Android UiAutomator: 配置UiSelector timeout时间
- android.useDeprecatedNdk=true 添入工程根目录下的新建 gradle.properties 文件
- JavaSE学习57:GUI编程之事件模型(二)
- JVM中显示锁基础AbstractQueuedSynchronizer
- iOS 【UIKit-导航控制器 UINavigationController】
- query中如何能保证页面新开一个窗口而不被浏览器给拦截
- Android UiAutomator: 断开USB运行脚本
- 关于priority_queue的一个问题
- 在使用JdbcTemplate.queryForMap(String )的时候发现一个问题.
- String和CharSequence
- JavaSE学习56:GUI编程之事件模型(一)
- mybatis_user_guide(8) 日志
- php中get、post和request的用法
- Android基础知识(1)——UI编程
- 基于STM32的Arduino开发板STAR Otto
- leetcode 357. Count Numbers with Unique Digits
- 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形…)
- php中include和require的区别详解
- mybatis_user_guide(7) SQL语句构建器类