UI_10 表视图的编辑、UITableViewController

读取plist文件并将其内容显示到表视图上。并添加编辑(增加,删除)、移动cell的操作。

plist文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "
http://www.apple.com/DTDs/PropertyList-1.0.dtd
">
<plist version="1.0">
<dict>
  <key>北京</key>
        <array>
           <dict>
                     <key>area</key>
                      <string>100万</string>
                <key>name</key>
                      <string>海淀区</string>
         </dict>
            <dict>
            <key>area</key>
            <string>101万</string>
                   <key>name</key>
                      <string>朝阳区</string>
         </dict>
     </array>
   <key>新疆</key>
        <array>
           <dict>
            <key>area</key>
            <string>112万</string>
                   <key>name</key>
                      <string>乌鲁木齐</string>
        </dict>
            <dict>
            <key>area</key>
            <string>113万</string>
                   <key>name</key>
                      <string>哈密</string>
          </dict>
            <dict>
            <key>area</key>
            <string>114万</string>
                   <key>name</key>
                      <string>吐鲁番</string>
         </dict>
     </array>
</dict>
</plist>

结构图如下:



?、tableView编辑

编辑的步骤如下:

  • 1、进入可编辑状态
  • 2、设置指定分区(section)中的行(row)是否可以被编辑
  • 3、设置指定分区(section)中的行(row)是什么类型的编辑样式
  • 4、编辑完成(一定要先操作数据,后操作UI)

//1、进入可编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
   
    [_tableView setEditing:editing animated:animated];
   
}

//2、设置指定分区(section)中的行(row)是否可以被编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!indexPath.section&&!indexPath.row) {
        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
{
    switch (editingStyle) {
        case UITableViewCellEditingStyleDelete:
            NSLog(@"Delete");
            //1.先删除数据
            NSString *key=_keysArray[indexPath.section];
            NSMutableArray *array = _allCityDic[key];
            [array removeObjectAtIndex:indexPath.row];
           
            //2.操作UI
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
           
            if (!array.count) {
                //1.先操作数据
                [_keysArray removeObject:key];
                [_allCityDic removeObjectForKey:key];
                //2.再操作UI
                [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationBottom];
            }
           
           
            break;
        case UITableViewCellEditingStyleNone:
            NSLog(@"None");
            break;
        case UITableViewCellEditingStyleInsert:
            NSLog(@"Insert");
           
            City *aCity = [[City alloc] init];
           
            aCity.name = @"丰台区";
            aCity.area = @"30万";
           
            //1.数据
            NSString *key1 = _keysArray[indexPath.section];
            NSMutableArray *cityArray = _allCityDic[key1];
            [cityArray insertObject:aCity atIndex:indexPath.row];
           
            //2.UI
            [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
           
            break;
        default:
            break;
    }

}

最后,不要忘了添加编辑按钮

self.navigationItem.rightBarButtonItem = self.editButtonItem;



?、tableView移动

步骤:

  • 1.让tableView处于可编辑状态(同上)
  • 2.设置指定的分区section中的行row是否可以被移动
  • 3.实现移动
  • 4.限制跨区移动

//1.让tableView处于可编辑状态(同上)

//2.设置指定的分区section中的行row是否可以被移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//3.实现移动
//方法:保存-删除-插入
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    if (sourceIndexPath.row!=destinationIndexPath.row) {
        NSString *key = _keysArray[sourceIndexPath.section];
        NSMutableArray *citiesArray = _allCityDic[key];
        //获取数组中要被移动的元素
        City *aCity = citiesArray[sourceIndexPath.row];
        //    //保存
        //    [aCity retain];
        //    //删除(必须retain)
        //    [citiesArray removeObjectAtIndex:sourceIndexPath.row];
        //    //移动
        //    [citiesArray insertObject:aCity atIndex:destinationIndexPath.row];
        //    [aCity release];
       
        [citiesArray insertObject:aCity atIndex:destinationIndexPath.row];
        [citiesArray removeObjectAtIndex:sourceIndexPath.row+1];
    }
   
}

//4.限制跨区移动
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    //如果目的地与出发地位置在同一区
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    } else {
        return sourceIndexPath;
    }

}



三、UITableViewController

注意:

  • UITableViewController继承?UIViewController,?带?个tableView
  • self.view不是UIView?是UITableView
  • datasource和delegate默认都是self(UITableViewController)
  • 开发中只需要建?UITableViewController?类
时间: 2024-08-24 13:09:07

UI_10 表视图的编辑、UITableViewController的相关文章

表视图控制器(TableViewController)(二)

1 tableView的编辑模式 1.1 问题 表视图可以进入编辑模式,当进入编辑模式就可以进行删除.插入.移动单元等操作,本案例还是使用联系人界面学习如何进入编辑模式,以及进入编辑模式之后的删除.插入.移动等操作,如图-1所示: 图-1 1.2 方案 首先还是创建一个带导航的TRContactTableViewController对象做为根视图控制器. 其次创建一个TRContact类用于管理联系人信息,有两个NSString类型的属性分别为name和phoneNumber,本案例为了学习方便

UI学习笔记---第十天UITableView表视图编辑

UITableView表视图编辑 表视图编辑的使用场景 当我们需要手动添加或者删除某条数据到tableView中的时候,就可以使用tableView编辑.比如微信 扣扣中删除和某人的通话 当我们需要手动调整单元格的顺序时,就可以通过tableView移动,移动单元格到指定位置 代理AppDelegate.m中代码 #import "AppDelegate.h" #import "RootViewController.h" @implementation AppDel

UITableView 表视图编辑

UITableViewController(表视图控制器)继承自UIViewController,自带一个tableView self.view不是UIView而是UITableView datasource和delegate你默认都是self(UITableViewController) 开发过程中只需建立UITableViewController子类 tableView编辑   tableView编辑:cell的添加.删除 使用的场景:删除一个下载好的视频,删除联系人. 插入一条新的聊天记录

表视图控制器(TableViewController)(一)

1 创建一个UITableViewController并展示简单数据 1.1 问题 有很多移动客户端的应用都是采用表的形式来展示数据,因为表视图能使数据看起来更规整.更有调理,比如微信界面就是使用的表视图,如图-1所示: 图-1 在IOS中表视图是非常重要的视图,类型名称为UITabelViewController,是UIViewController的子类,本案例将学习如何使用UITableViewController来展示简单的数据,完成效果如图-2所示: 图-2 1.2 方案 首先创建一个S

IOS开发之表视图爱上CoreData

在接触到CoreData时,感觉就是苹果封装的一个ORM.CoreData负责在Model的实体和sqllite建立关联,数据模型的实体类就相当于Java中的JavaBean, 而CoreData的功能和JavaEE中的Hibernate的功能类似,最基本是两者都有通过对实体的操作来实现对数据库的CURD操作.CoreData中的上下文(managedObjectContext)就相当于Hibernate中的session对象, CoreData中的save操作就和Hibernate中的comm

第09章 导航控制器和表视图

总结: 其实navigationController很简单,其往往就是应用程序委托的根视图控制器 且其往往就是通过第一个显示的视图控制器来初始化自己. 而后的工作,就是pushViewController,popViewController... [self.navigationController   pushViewController: controller animated:YES]; [self.navigationController popViewController Animat

IOS开发指南 第6章 表视图 学习

1 概述 结构:表头视图(table header view),表脚视图(table footer view),节(section),单元格(cell) 相关类:UITableViewCell UITableViewController UITableViewHeaderFooterView 委托协议和数据源协议 分类:普通表视图:用于动态表 分组表视图:用于静态表,进行界面布局 单元格的组成:图标 标题 拓展视图 样式:拓展视图由枚举类型UITableViewCellAccessoryType

表视图搜索栏

一.效果 1. 启动程序:上面搜索栏.右边索引条 2. 点击表单元有警告框提示 3. 点击搜索栏输入内容可根据长短范围搜索 二.分析 1. 创建一个表视图,指定委托,实现表单元的显示 2. 创建一个显示搜索结果的模型,该模型也是表视图,遵循搜索结果更新协议,作为更新器 3. 创建一个搜索控制器,指定搜索结果控制器,并创建搜索栏,让搜索结果控制器实现同步更新协议,让搜索到的内容得以在搜索结果中更新 4. 在更新协议实现方法中配置搜索的逻辑 三.实现 1. 实现文件 2. Main.storyboa

表视图控制器(TableViewController)(三) 、 表视图搜索

1 乐库的设置界面 1.1 问题 tableView分为静态(static)和动态(dynamic),之前使用的都是动态的tableView,表视图的有多少分区.有多少行以及每一行显示的内容都不是固定的,都由数据模式来决定.而静态的tableView有多少分区.有多少行以及每一行显示的内容都是固定不变的. 静态tableView应用广泛,常用于各种应用软件的设置界面,本案例使用静态tableView完成乐库的设置界面,如图-1所示: 图-1 1.2 方案 由图-1可以看到该界面的显示内容是固定不