iOS学习笔记—— UItableView 控件的简单使用
来源:互联网 发布:js微信通讯录字母索引 编辑:程序博客网 时间:2024/05/22 18:38
UITableView 可以说是iOS开发中最常用的控件,除了游戏之外,几乎所有的应用中独会出现他的身影。
使用UITableView控件需要遵守两种协议 UITableViewDelegate和 UITableViewDataSource。
常用方法如下:
1.返回(每个分区)表单元个数(行数)
- (NSInteger) tableView: (UItableView *) tableVIew
numberOfRowsInSection: (NSInteger)section
2.返回表单元信息
- (UITableViewCell) tableView: (UITableVIew *) tableView
cellForRowAtIndexPath: (NSInteger) indexPath
3.分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
设置分区名称
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
4.选中某行后的响应函数
- (void) tableVIew: (UITableView *)tableView
didSelectRowAtIndexPath: (NSIndexPath *) indexPath
5. 编辑表单元(添加、删除)
- (void) tableView: (UITableView) tableView
commotEditingStyle: (UITableViewCellEditingStytle)editingStytle
forRowAtIndexPath: (NSIndexPath *) indexPath
.p.s 指定删除按钮标题
- (NSString *) tableView: (UItableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath: (NSIndexPath *)indexPath
6.表单元移动
- (void)tableView: (UITableView *) tableView
moveRowAtIndexPath: (NSIndexPath *) fromIndexPath
toIndexPath: (NSIndexPath *) toIndexPath
.p.s 允许移动
- (BOOL)tableView: (UITableView *) tableView
canMoveRowAtIndexPath: (NSIndexPath *) indexPath
.p.s 允许编辑
-(BOOL)tableView: (UITableView *) tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath
7.右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
8.获取某一行的信息
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
9.设置表单元高度(行高)
- (CGFloat) tableView: (UItableView *) tableView
heightForRowAtIndexPath: (NSIndexPath *) indexPath
10.设置单元格颜色
- (void) tableView: (UITableView *) tableVIew
willDisplayCell: (UITableViewCell *) cell
forRowAtIndexPath: (NSIndexPath *) indexPath
11. 设置表单元文本缩进
- (NSInteger) tableVIew: (UITableView *) tableView
indentationLevelForRowAtIndexPath: (NSIndexPath *) indexPath
12. 添加页眉、页脚
- (NSString *) tableView: (UITableView) tableView
titleForHeaderInSection: (NSInteger) section
- (NSString *) tableView: (UITableView) tableView
titleForFooterInSection: (NSInteger) section
下为常用代码片段:
1.设置表单元信息
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellWithIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier]; } NSUInteger row = [indexPath row]; cell.textLabel.text = [dataList objectAtIndex:row]; return cell;}
2. 进入编辑状态(删除,移动)
- (IBAction)Del:(id)sender { [_tableView setEditing:!_tableView.editing animated:YES]; if (_tableView.editing) [_DeleteButtonTitle setTitle:@"Done" forState:UIControlStateNormal]; else [_DeleteButtonTitle setTitle:@"Delete" forState:UIControlStateNormal];}
3.左滑删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ // 左滑删除 if (editingStyle == UITableViewCellEditingStyleDelete) { [dataList removeObjectAtIndex: indexPath.row]; [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationAutomatic]; }}
4.选中并标记某行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 选中响应函数(标记) UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath]; if (cellView.accessoryType == UITableViewCellAccessoryNone) cellView.accessoryType = UITableViewCellAccessoryCheckmark; else cellView.accessoryType = UITableViewCellAccessoryNone;}5.移动代码
- (BOOL)tableView:(UITableView *)tableViewcanMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES;}- (void)tableView:(UITableView *)tableViewmoveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { NSUInteger fromRow = [fromIndexPath row]; NSUInteger toRow = [toIndexPath row]; id object = [dataList objectAtIndex:fromRow]; [dataList removeObjectAtIndex:fromRow]; [dataList insertObject:object atIndex:toRow];}
- iOS学习笔记—— UItableView 控件的简单使用
- iOS学习笔记—— UIPickerView 控件的简单使用
- iOS学习笔记—— UISegmentedControl 控件的简单使用
- iOS—简单封装UITableView使用1
- iOS—简单封装UITableView使用2
- iOS开发学习笔记——UITableview
- IOS学习笔记——UITableView(一)
- IOS学习笔记——UITableView (二)
- IOS学习笔记——UITableView(三)
- iOS开发UI篇—UITableview控件简单介绍
- iOS开发UI篇—UITableview控件简单介绍
- iOS开发UI基础—18UITableview控件简单介绍
- iOS开发UI篇—UITableview控件简单介绍
- iOS开发UI篇—UITableview控件简单介绍
- iOS开发UI篇—UITableview控件简单介绍
- (温故而知新)iOS开发UI篇—UITableview控件简单介绍
- iOS开发UI篇—UITableview控件基本使用
- iOS开发UI篇—UITableview控件使用小结
- POJ 3468 伸展树建树
- 重温线性代数(2)——向量空间
- 如何实现函数执行完,但是结果不被系统回收?
- oracle字符串聚合
- KMP算法
- iOS学习笔记—— UItableView 控件的简单使用
- 如何使用cntlm配置代理上网
- 言诟杭铣戏思思檀杭陡盖
- FPGA综合编码
- Hough变换直线检测
- java version : 找出数组中两个只出现一次的数字
- HDU 4870 Rating (概率+高斯消元)
- hibernate学习之——many-to-many(双向多对多关系)
- Spring 注解学习手札(一) 构建简单Web应用