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 *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(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];
}