UITableView
来源:互联网 发布:微信加友软件免费下载 编辑:程序博客网 时间:2024/05/01 09:27
在IOS开发中,最常用的UI控件,应该就是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;}
0 0
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableview
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView,
- UITableView
- UITableView
- UITableView
- UITableView
- 国外免费版杀毒软件
- 今夜有感
- Redis-server在windows下闪退
- Struts2+Spring3+Hibernate4+JavaEE6 配置
- Java中动态代理使用
- UITableView
- KNN算法在保险业精准营销中的应用
- 五子棋 手打稍加改变自慕课网hyman
- Oracle学习02【持续更新】
- LNMP环境一键安装
- 1021. 个位数统计 (15) PAT 乙级
- 怎么用dos的cmd命令行查看网络连接打开的端口及服务
- Android富文本编辑器总结(三)
- R语言的基本操作