• tableView操作数据持久化


    self.navigationItem.rightBarButtonItem=self.editButtonItem;

        //创建文件管理器

        NSFileManager *fileManager=[NSFileManager defaultManager];

        //判断路径下是否存在文件

        if ([fileManager fileExistsAtPath:PATH])

        {

            self.Marr=[NSMutableArray arrayWithContentsOfFile:PATH];

            

        }

        else

            //给文件添加数据

        {

            [self.Marr addObject:@"aaa"];

            [self.Marr addObject:@"www"];

            [self.Marr addObject:@"zzz"];

        }

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];

    cell.textLabel.text=self.Marr[indexPath.row];

     return cell;

    }

    //删除tableView单元格的方法

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

        //                      删除表视图单元格编辑界面风格

        if (editingStyle == UITableViewCellEditingStyleDelete)

        {

            //                         集合指定位置索引

            [self.Marr removeObjectAtIndex:indexPath.row];

    //        q                 从数据源中删除行

            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        }

        //               插入表视图单元格编辑风格  标示图前面 +  -的风格

        else if (editingStyle == UITableViewCellEditingStyleInsert)

        {

            // 创建一个新的合适的类的实例,将其插入到数组,并添加一个新行表视图

            [self.Marr addObject:@"lamco"];

            

            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        }

        //数据持久化(激素hi把文件放到Directory文件中)

        [self.Marr writeToFile:PATH atomically:YES];

    }

    //移动tableView

    // Override to support rearranging the table view.

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

    {

    //    交互的四步

    //    1、获取交互元素   赋给一个临时变量

    //    2、从集合中删除交互位置的元素

    //    3、叫临时字符串插入到指定位置

        NSString *tempstr=[self.Marr objectAtIndex:fromIndexPath.row];

        [self.Marr removeObject:tempstr];

        [self.Marr insertObject:tempstr atIndex:fromIndexPath.row];

        

        //数据持久化

        [self.Marr writeToFile:PATH atomically:YES];

    }

  • 相关阅读:
    jdk8u241各系统版本
    jdk8u241各系统版本
    【转】软件测试测试用例设计规范
    虚拟机安装Centos(VirtulBox)
    Java Selenium搭建Web UI自动化环境
    【转】WEB(Javascript)远程调用方案清单
    关于.net页面提交后css失效或部分失效的问题
    常用照片尺寸 照片规格(英寸) (厘米) (像素) 数码相机类型
    【转】jquery的autoComplete 中文介绍
    网页 乱码
  • 原文地址:https://www.cnblogs.com/wrzheng/p/5330601.html
Copyright © 2020-2023  润新知