UITableView基础 UITableView编辑,UITableViewCell自定义
来源:互联网 发布:php httprequest类 编辑:程序博客网 时间:2024/05/21 10:34
每个格子是一个UITableViewCell,
cell的位置由row(行), section( 分区)决定
//注册cell
[self.tableViewregisterClass:[MyCellclass]forCellReuseIdentifier:@"reuse"];
//数据源协议和代理
tableView.dataSource= self;
tableView.delegate= self;
//分割线的颜色
tableView.delegate= self;
//分割线的颜色
tableView.separatorColor= [UIColorredColor];
tableView.separatorStyle //设置tableView的那条线
//统一设置cell的高度
tableView.rowHeight= 50;
//给tableview添加一个顶部view
UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100,200)];
view.backgroundColor= [UIColorgrayColor];
tableView.tableHeaderView= view;
tableView.rowHeight= 50;
//给tableview添加一个顶部view
UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100,200)];
view.backgroundColor= [UIColorgrayColor];
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 = [tableViewdequeueReusableCellWithIdentifier:str];
//2.判断是否取得一个cell
if (cell == nil) {
//如果取得的cell是nil,就创建一个新的cell
cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:str]autorelease];
NSLog(@"需要新的cell");
}
NSString *name = [self.arrobjectAtIndex:indexPath.row];
//3.对cell重新赋值,使用
cell.textLabel.text= name;
cell.detailTextLabel.text= [NSStringstringWithFormat:@"section : %d row : %d", indexPath.section, indexPath.row];
return cell;
}
//告诉tableview每个分区(section)显示多少行(row)
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
//提供数据的数组的行数
return [self.arrcount];
}
//返回多个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return 10;
}
//设置分区的顶部显示的文字
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSStringstringWithFormat:@"section : %d", section];
}
//设置分区顶部的高度
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
//自定义一个分区的顶部view
- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * view = [[UIViewalloc]init];
view.backgroundColor= [UIColoryellowColor];
return [view autorelease];
}
//处理点击cell的事件
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@"%@", [self.arrobjectAtIndex: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 = [tableViewdequeueReusableCellWithIdentifier:str];
//2.判断是否取得一个cell
if (cell == nil) {
//如果取得的cell是nil,就创建一个新的cell
cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:str]autorelease];
NSLog(@"需要新的cell");
}
NSString *name = [self.arrobjectAtIndex:indexPath.row];
//3.对cell重新赋值,使用
cell.textLabel.text= name;
cell.detailTextLabel.text= [NSStringstringWithFormat:@"section : %d row : %d", indexPath.section, indexPath.row];
return cell;
}
//告诉tableview每个分区(section)显示多少行(row)
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
//提供数据的数组的行数
return [self.arrcount];
}
//返回多个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return 10;
}
//设置分区的顶部显示的文字
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSStringstringWithFormat:@"section : %d", section];
}
//设置分区顶部的高度
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
//自定义一个分区的顶部view
- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * view = [[UIViewalloc]init];
view.backgroundColor= [UIColoryellowColor];
return [view autorelease];
}
//处理点击cell的事件
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@"%@", [self.arrobjectAtIndex: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截图
0 0
- UITableView基础 UITableView编辑,UITableViewCell自定义
- UITableView自定义UITableViewCell
- UITableView 自定义UITableViewCell
- UITableView,自定义UITableViewCell
- UITableView 系列: 自定义 UITableViewCell
- UITableView属性 自定义UITableViewCell
- UITableView 系列之自定义 UITableViewCell
- UITableView 系列之自定义 UITableViewCell
- swift UItableView和自定义uitableViewCell
- UI基础-UITableView 编辑
- UITableView&UITableViewCell
- Swift中UITableView的用法(自定义UITableView和UITableViewCell)
- UITableView 系列五 :自定义UITableViewCell (实例)
- UITableView 系列五 :自定义UITableViewCell (实例)
- UITableView详解(UITableViewCell(二) 自定义cell)
- UITableView使用<1>之自定义UITableViewCell
- UITableView之(五):自定义UITableViewCell
- UI基础之---- UITableView 编辑
- 解决eclipse闪退或者启动不了
- 安装.NET Freamwork 提示error 25015,原因是出现了系统错误:另一个程序正在使用此文件
- 工具类
- OC基础—类的私有方法和私有变量
- HDOJ 还是畅通工程 1233
- UITableView基础 UITableView编辑,UITableViewCell自定义
- mac cornerstone could not contact repository to read the latest log entries
- jdbc基础 (二) 通过properties配置文件连接数据库
- poj 2078 Matrix
- 通过经度、纬度获取地址名称[JS实现]
- Error: String types not allowed (at 'layout_gravity' with value 'bottom/center_horizontal').
- return null; 和 return;有什么区别
- PHP OPENSSL 函数手册翻译备查 -- openssl_get_md_methods
- iOS开发中最有用关键的代码合集(2)