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.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;


//协议中的方法
//返回每行显示的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) {
       
//如果取得的cellnil,就创建一个新的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];


//系统提供一个编辑按钮
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.rowretain];
    
//将数据从原来的位置移除掉
    [
self.arr removeObjectAtIndex:sourceIndexPath.row];
    
//把数据放在目的位置上
    [
self.arr insertObject:str atIndex:destinationIndexPath.row];
    
    [str 
release];
}


UITableViewCell自定义

//第一步
//创建自己的两个视图属性,属性名不要和系统的属性重名(imageviewtextlabledetailTextLabel
@property (nonatomicretain)UILabel *nameLable;

//第二步
//初始化方法  初始化自己的视图对象, 进行简单的设置  不设置fram
self.nameLable = [[UILabel allocinit];
        
self.nameLable.backgroundColor = [UIColor magentaColor];
        [
self.contentView addSubview:self.nameLable];
        [
_nameLable release];

//第三步
- (void)layoutSubviews
{
    [
super layoutSubviews];
    
//cell在这个方法中对所有的子视图重新布局
    
//cell显示到tableview上之前 最后调用的一个方法
    
self.nameLable.frame = CGRectMake(00self.contentView.frame.size.width/2self.contentView.frame.size.height);
}


PPT截图





0 0