zoukankan      html  css  js  c++  java
  • ios开发笔记-1-tableView

    tableview的编辑是通过[self.tableview setEditing: BOOL1 animotion: BOOL2];进入的,如果需要进入编辑模式,则调用方法,将BOOL1改为YES.如果要退出编辑模式,则调用方法,将BOOL1改为NO.BOOL2为是否使用动画.

    如果你想将编辑这个button由文字改为图片,想通过setBackGroudImage这个方法替换文字,你会发现图片会因为无法设置frame而产生拉伸,效果不理想.解决方法:你可以自定义rightBarButtonItem,重写代理方法.下面将tableview的编辑的代码贴出来,与大家分享

    - (void)viewDidLoad {

        [super viewDidLoad];

        //编辑按钮

        self.navigationItem.rightBarButtonItem = self.editButtonItem;

        self.navigationItem.rightBarButtonItem.title = @"编辑";

        self.navigationItem.rightBarButtonItem.tintColor=[UIColor blackColor];

    }

    #pragma mark---------tableView的编辑(删除,插入)

    //1.点击编辑按钮

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

        [super setEditing:editing animated:animated];

        // Don't show the Back button while editing.

    //    [self.navigationItem setHidesBackButton:editing animated:YES];

        if (editing) {

            self.navigationItem.rightBarButtonItem.title = @"完成";

            NSLog(@"abc");

        }else {//点击完成按钮

            self.navigationItem.rightBarButtonItem.title = @"编辑";

            NSLog(@"123");

        }

        [_customView.tableView setEditing:editing animated:YES];

    }

    //2.设置定制分区(section)中的行(row)是否可以被编辑

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

    {

    //    if (indexPath.section==0) {

    //        return NO;

    //    }

        return YES;

    //3.设置指定的分区(section)的行(row)编辑的样式(添加,删除)

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

    {

        if (indexPath.section==1) {

            return UITableViewCellEditingStyleInsert;

        }

        return UITableViewCellEditingStyleDelete;

    }

    }

    //4.编辑完成(先操作数据源,再修改UI)

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

    {

        if (editingStyle==UITableViewCellEditingStyleDelete) {

            NSLog(@"删除");

            //同步更新beginUpdates 和 endUpdates 之间修改的UI和数据

            [_customView.tableView beginUpdates];

            

            //1.删除数据

            NSString *key=_allDataDic.allKeys[indexPath.section];

            NSMutableArray *array=_allDataDic[key];

            [array removeObjectAtIndex:indexPath.row];

            //2.删除UI

            //删除行

            [_customView.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

            if (array.count==0) {

                

                //删除区头上的数据(删除key对应的小数组)

                NSString *key=_allDataDic.allKeys[indexPath.section];

                [_allDataDic removeObjectForKey:key];

                //当某个分区中只剩一个cell的时候(或者小数组中只剩一个数据的时候),删除时,也要把区头删除掉

                NSIndexSet *indexSet=[NSIndexSet indexSetWithIndex:indexPath.section];

                [_customView.tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationLeft];

            }

            

            [_customView.tableView endUpdates];

            

        }else if (editingStyle==UITableViewCellEditingStyleInsert)

        {

            NSLog(@"插入");

            //1.插入数据

            NSString *key=_allDataDic.allKeys[indexPath.section];

            NSMutableArray *array=_allDataDic[key];

            [array insertObject:@"昌平" atIndex:indexPath.row];

            //2.插入UI(cell)        

          //  NSIndexPath *indexPath1=[NSIndexPath indexPathForRow:0 inSection:indexPath.section];//保证每次插入数据都在第一行

            [_customView.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];        

        }

    }

    转载:http://www.cnblogs.com/Twisted-Fate/p/4727766.html

  • 相关阅读:
    显卡,显卡驱动,nvcc, cuda driver,cudatoolkit,cudnn到底是什么?
    安装cuda之后没有安装nvidia显卡驱动可能会出现ImportError: libcuda.so.1: cannot open shared object file: No such file or directory
    ubuntu安装
    老男孩Linux查看硬盘使用个情况及其挂载点
    Anaconda使用conda activate激活环境出错CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
    Linux,ubuntu设置anaconda的环境变量
    Linux 磁盘管理
    anaconda路径改变后对其中文件的修改
    Linux 文件基本属性
    川藏游记发在别处的
  • 原文地址:https://www.cnblogs.com/feng520/p/4962227.html
Copyright © 2011-2022 走看看