每个格子是一个UITableViewCell,
cell的位置由row(行), section( 分区)决定
//注册cell
[self.tableView
registerClass:[MyCell
class]
forCellReuseIdentifier:@"reuse"];
//数据源协议和代理
tableView.dataSource
=
self;
tableView.delegate = self;
//分割线的颜色
tableView.delegate = self;
//分割线的颜色
tableView.separatorColor
= [UIColor
redColor];
tableView.separatorStyle //设置tableView的那条线
//统一设置cell的高度
tableView.rowHeight = 50;
//给tableview添加一个顶部view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
view.backgroundColor = [UIColor grayColor];
tableView.tableHeaderView = view;
tableView.rowHeight = 50;
//给tableview添加一个顶部view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
view.backgroundColor = [UIColor grayColor];
tableView.tableHeaderView = view;
//协议中的方法
//返回每行显示的cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell的重用机制
//每当一个tableview要显示一个cell的时候,系统都会调用这个方法给tableview提供一个新cell
//每个tableview内部都有若干个cell的重用池,每当需要cell的时候,都去某个重用池中取得一个cell
//如果重用池中有cell, 就直接使用;如果没有,就创建一个新cell,给cell一个重用标志, 便于系统区分。
//1.从重用池中尝试获取一个cell
static NSString *str = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
//2.判断是否取得一个cell
if (cell == nil) {
//如果取得的cell是nil,就创建一个新的cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
NSLog(@"需要新的cell");
}
NSString *name = [self.arr objectAtIndex:indexPath.row];
//3.对cell重新赋值, 使用
cell.textLabel.text = name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"section : %d row : %d", indexPath.section, indexPath.row];
return cell;
}
//告诉tableview每个分区(section)显示多少行(row)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//提供数据的数组的行数
return [self.arr count];
}
//返回多个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
//设置分区的顶部显示的文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"section : %d", section];
}
//设置分区顶部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
//自定义一个分区的顶部view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * view = [[UIView alloc] init];
view.backgroundColor = [UIColor yellowColor];
return [view autorelease];
}
//处理点击cell的事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@", [self.arr objectAtIndex:indexPath.row]);
}
//取消选中时触发的方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell的重用机制
//每当一个tableview要显示一个cell的时候,系统都会调用这个方法给tableview提供一个新cell
//每个tableview内部都有若干个cell的重用池,每当需要cell的时候,都去某个重用池中取得一个cell
//如果重用池中有cell, 就直接使用;如果没有,就创建一个新cell,给cell一个重用标志, 便于系统区分。
//1.从重用池中尝试获取一个cell
static NSString *str = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
//2.判断是否取得一个cell
if (cell == nil) {
//如果取得的cell是nil,就创建一个新的cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
NSLog(@"需要新的cell");
}
NSString *name = [self.arr objectAtIndex:indexPath.row];
//3.对cell重新赋值, 使用
cell.textLabel.text = name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"section : %d row : %d", indexPath.section, indexPath.row];
return cell;
}
//告诉tableview每个分区(section)显示多少行(row)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//提供数据的数组的行数
return [self.arr count];
}
//返回多个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
//设置分区的顶部显示的文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"section : %d", section];
}
//设置分区顶部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
//自定义一个分区的顶部view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * view = [[UIView alloc] init];
view.backgroundColor = [UIColor yellowColor];
return [view autorelease];
}
//处理点击cell的事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@", [self.arr objectAtIndex:indexPath.row]);
}
//取消选中时触发的方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
UITableView编辑
//开启tableview的编辑模式
[tableView setEditing:YES animated:YES];
[tableView setEditing:YES animated:YES];
//系统提供一个编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//点击删除触发的方法
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//对进行的操作的判断(添加/删除)
if (editingStyle == UITableViewCellEditingStyleDelete) {
//写代码删除相应的cell
//在删除cell之前,一定要删除数据源里面相应的内容
[self.arr removeObjectAtIndex:indexPath.row];
NSArray *arr = [NSArray arrayWithObjects:indexPath, nil];
//参数1: 要删除的indexpath组成的数组
//参数2: 删除时展现的动画动画效果
[tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationLeft];
}else if (editingStyle == UITableViewCellEditingStyleInsert){
// [self.arr addObject:<#(id)#>]
}
}
//点击编辑按钮 系统会自动调用这个方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
//利用系统的编辑按钮 改变tableview的编辑状态
[self.tableView setEditing:editing animated:YES];
}
//改变cell的编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.row == 0) {
// return UITableViewCellEditingStyleDelete;
// }
return UITableViewCellEditingStyleInsert;
}
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
// //是否能够被编辑
// return NO;
//}
//移动数据调用的方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSLog(@"%d %d", sourceIndexPath.row, destinationIndexPath.row);
//获取要移动的数据
id str = [[self.arr objectAtIndex:sourceIndexPath.row] retain];
//将数据从原来的位置移除掉
[self.arr removeObjectAtIndex:sourceIndexPath.row];
//把数据放在目的位置上
[self.arr insertObject:str atIndex:destinationIndexPath.row];
[str release];
}
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//对进行的操作的判断(添加/删除)
if (editingStyle == UITableViewCellEditingStyleDelete) {
//写代码删除相应的cell
//在删除cell之前,一定要删除数据源里面相应的内容
[self.arr removeObjectAtIndex:indexPath.row];
NSArray *arr = [NSArray arrayWithObjects:indexPath, nil];
//参数1: 要删除的indexpath组成的数组
//参数2: 删除时展现的动画动画效果
[tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationLeft];
}else if (editingStyle == UITableViewCellEditingStyleInsert){
// [self.arr addObject:<#(id)#>]
}
}
//点击编辑按钮 系统会自动调用这个方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
//利用系统的编辑按钮 改变tableview的编辑状态
[self.tableView setEditing:editing animated:YES];
}
//改变cell的编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// if (indexPath.row == 0) {
// return UITableViewCellEditingStyleDelete;
// }
return UITableViewCellEditingStyleInsert;
}
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
// //是否能够被编辑
// return NO;
//}
//移动数据调用的方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSLog(@"%d %d", sourceIndexPath.row, destinationIndexPath.row);
//获取要移动的数据
id str = [[self.arr objectAtIndex:sourceIndexPath.row] retain];
//将数据从原来的位置移除掉
[self.arr removeObjectAtIndex:sourceIndexPath.row];
//把数据放在目的位置上
[self.arr insertObject:str atIndex:destinationIndexPath.row];
[str release];
}
UITableViewCell自定义
//第一步
//创建自己的两个视图属性,属性名不要和系统的属性重名(imageview,textlable,detailTextLabel)@property (nonatomic, retain)UILabel *nameLable;
//第二步
//初始化方法 初始化自己的视图对象, 进行简单的设置
不设置fram
self.nameLable =
[[UILabel alloc] init];
self.nameLable.backgroundColor = [UIColor magentaColor];
[self.contentView addSubview:self.nameLable];
[_nameLable release];
self.nameLable.backgroundColor = [UIColor magentaColor];
[self.contentView addSubview:self.nameLable];
[_nameLable release];
//第三步
- (void)layoutSubviews
{
[super layoutSubviews];
//cell在这个方法中对所有的子视图重新布局
//在cell显示到tableview上之前 最后调用的一个方法
self.nameLable.frame = CGRectMake(0, 0, self.contentView.frame.size.width/2, self.contentView.frame.size.height);
}
{
[super layoutSubviews];
//cell在这个方法中对所有的子视图重新布局
//在cell显示到tableview上之前 最后调用的一个方法
self.nameLable.frame = CGRectMake(0, 0, self.contentView.frame.size.width/2, self.contentView.frame.size.height);
}
PPT截图

本文详细解析如何在iOS应用中自定义UITableViewCell并实现UITableView的编辑功能,包括数据源、代理方法、编辑模式设置及点击事件处理。
2347

被折叠的 条评论
为什么被折叠?



