zoukankan      html  css  js  c++  java
  • UI控件之UITableView的协议方法

    <UITableViewDataSource,UITableViewDelegate>

    //设置表视图的编辑状态

    -(void)setEditing:(BOOL)editing animated:(BOOL)animated

    {

        [super setEditing:editing animated:animated];

        [_tableView setEditing:editing animated:animated];

    }

    //设置编辑模式(插入、删除、选择),默认是删除

    -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return UITableViewCellEditingStyleInsert;

    }

    //设置编辑内容(插入、删除

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    {

        if(editingStyle==UITableViewCellEditingStyleDelete)

        {

            //如果是删除的话。。。。

        }

        if(editingStyle==UITableViewCellEditingStyleInsert){

            //将数据加入到数据源

            [_dataArray insertObject:@"test" atIndex:indexPath.row];

            //更新界面

            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

        }

    }

    //设置是否允许移动单元格

    -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return YES;

    }

    //移动单元格执行的操作第1个参数是操作的表视图对象,第2个参数是单元格原来所在的行,第3个参数是单元格移动后新的行

    -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

    {

        //修改数据源,将moveRowAtIndexPath所在的数据删除,插入到destinationIndexPath所在的位置上

        NSString *str=_dataArray[sourceIndexPath.row];

        [_dataArray removeObjectAtIndex:sourceIndexPath.row];

        [_dataArray insertObject:str atIndex:destinationIndexPath.row];

    }

    //当选择一行数据,当表视图处于编辑状态时将其数据添加到删除数组中

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

        if(_tableView.editing){

            NSString *str=_dataArray[indexPath.row];

            if(![_removeArray containsObject:str]){

                [_removeArray addObject:str];

            }

        }

    }

    //当用户取消选择某行数据时,将该行数据从删除数组中移除

    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

    {

        //判断表视图是否处于编辑状态

        if(_tableView.editing){

            NSString *str=_dataArray[indexPath.row];

            if([_removeArray containsObject:str]){

                [_removeArray removeObject:str];

            }

        }

    }

    //设置编辑模式(选择)

    -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        //插入和删除一起设置效果为选择模式

        return UITableViewCellEditingStyleInsert|UITableViewCellEditingStyleDelete;

    }

    //返回分组数,默认是1

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        return 1;

    }

    //返回cell的个数

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        return _dataArray.count;

    }

    //设置每个cell的内容

    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        static NSString *identifier=@"cell";

        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];

        if(cell==nil){

            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

        }

        cell.textLabel.text=_dataArray[indexPath.row];

        return cell;

    }

    http://www.cnblogs.com/PaulpauL/ 版权声明:本文为博主原创文章,未经博主允许不得转载。
  • 相关阅读:
    【Blog怎么玩】如何修改博客地址栏logo—怎样查找和制作ico文件
    【Blog怎么玩】怎么给博客添加支付宝/微信打赏按钮—tctip打赏插件开源
    【免费开源网站】利用乐云xzc.cn结合百度网盘来收作业
    【数字信号处理不挂科-笔记】第三讲-离散傅立叶变换
    【数字信号处理不挂科-笔记】第一讲-离散时间信号与系统
    【Blog怎么玩】学长教你怎么在博客里播放音乐+krc转lrc(krc2lrc)
    【南工程开源计划-通信网】南工程通信09级A卷-通信网
    【南工程开源计划-通信网】南工程通信07级A卷-通信网
    【南工程开源计划-通信网】南工程通卓15级B卷-电力系统通信网期末考试
    【南工程开源计划-通信网】南工程通卓17级A卷-通信网期末考试
  • 原文地址:https://www.cnblogs.com/PaulpauL/p/4890080.html
Copyright © 2011-2022 走看看