zoukankan      html  css  js  c++  java
  • UITableView 编辑

    对 UITableView 进行添加,删除,移动,基本操作的流程.

    1.初始化 UITableView 步骤:

      1> 遵守协议 <UITableViewDelegate,UITableViewDataSource>

      2> 设置代理

      3> 实现方法 必须实现的方法有两个 - (NSInteger)tableView:numberOfRowsInSection:(设置每个分组的行数)- (UITableViewCell*)tableView: cellForRowAtIndexPath: (设置每行的显示)

    2.对 tableView 进行增删移动操作 (通过实现协议方法)

      1>设置是否可编译 - (BOOL)tableView: canEditRowAtIndexPath: 返回 YES 可以编辑,返回 NO 不可编辑

      2>设置编辑的类型 - (UITableViewCellEditingStyle)tableView: editingStyleForRowAtIndexPath: 返回 UITableViewCellEditingStyleDelete 删除操作,UITableViewCellEditingStyleInsert 添加操作

      3>完成编辑操作:

        添加和删除操作相似,实现 - (void)tableView: commitEditingStyle: forRowAtIndexPath: 在进行操作时,一定是先对数据操作,然后操作 Cell

          删除操作:(实例方法)deleteSections: withRowAnimation: 删除分组  deleteRowsAtIndexPaths: withRowAnimation: 删除行

          添加操作:(实例方法)insertRowsAtIndexPaths: withRowAnimation: 添加行

        移动操作:- (void)tableView: moveRowAtIndexPath: toIndexPath:在进行操作时,一定是先对数据操作,然后操作 Cell

          移动操作:数据处理过程:先存储要移动数据,删除原数据,插入数据

          调用实例方法:moveRowAtIndexPath: toIndexPath: 两个参数 原位置,要移动到的位置

          限定移动的范围:- (NSIndexPath*)tableView: targetIndexPathForMoveFromRowAtIndexPath: (如果限定在同一分组内移动,判断传入的参数的 section 是否相等,(第一个参数原位置,第二个参数要移动的位置),相等返回要移动到的位置,不同返回原位置.

    个人写的一个简单的实现代码:

      1 //
      2 //  RootViewController.m
      3 //  Lesson10_HomeWork
      4 //
      5 //  Created by Ager on 15/10/26.
      6 //  Copyright © 2015年 Ager. All rights reserved.
      7 //
      8 
      9 #import "RootViewController.h"
     10 
     11 @interface RootViewController ()
     12 {
     13     UITableViewCellEditingStyle style; //表示对 table 的操作类型
     14     BOOL addFlag; //表示 addButton 按钮的状态
     15 }
     16 
     17 @end
     18 
     19 @implementation RootViewController
     20 
     21 - (void)loadView{
     22     self.rootView = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
     23     self.view = self.rootView;
     24 }
     25 
     26 - (void)viewDidLoad {
     27     [super viewDidLoad];
     28     // Do any additional setup after loading the view.
     29     
     30     //设置代理
     31     self.rootView.tableView.delegate = self;
     32     self.rootView.tableView.dataSource = self;
     33     
     34     //初始化数据
     35     self.DataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DataArray" ofType:@"plist"]];
     36     
     37     //添加删除触发按钮
     38     self.navigationItem.rightBarButtonItem = self.editButtonItem;
     39     self.editButtonItem.title = @"删除";
     40     
     41     //添加 添加数据按钮
     42     UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(insertAction:)];
     43     addFlag = NO;
     44     self.navigationItem.leftBarButtonItem = addButton;
     45     
     46     
     47 }
     48 
     49 #pragma mark --- 实现代理方法 ---
     50 
     51 #pragma mark --- 必须实现的方法 ---
     52 
     53 
     54 /**
     55  *  每组数据的行数
     56  */
     57 
     58 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     59     return [[self.DataArray objectAtIndex:section] count];
     60 }
     61 
     62 
     63 /**
     64  *  设置cell
     65  */
     66 
     67 - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     68     static NSString *cell_id = @"cell_id";
     69     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
     70     if (!cell) {
     71         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id];
     72     }
     73     
     74     cell.textLabel.text = [[self.DataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
     75     return cell;
     76 }
     77 
     78 
     79 #pragma mark --- 不必须实现的代理方法 ---
     80 
     81 /**
     82  *  分组数
     83  */
     84 
     85 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     86     return [self.DataArray count];
     87 }
     88 
     89 
     90 #pragma mark --- 对 TableView 编辑 ---
     91 
     92 
     93 /**
     94  *  设置是否可以编辑
     95  */
     96 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
     97     return YES;
     98 }
     99 
    100 
    101 /**
    102  *  设置编辑类型
    103  */
    104 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    105     return style;
    106 }
    107 
    108 
    109 /**
    110  *  完成 TableView 操作
    111  */
    112 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    113     //修改数据,修改UI
    114     //先修改数据,在修改UI
    115     
    116     if (editingStyle == UITableViewCellEditingStyleDelete) {
    117         //删除行
    118         if ([[self.DataArray objectAtIndex:indexPath.section] count] == 1) {
    119             //删除分组
    120             [self.DataArray removeObjectAtIndex:indexPath.section];
    121             [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];
    122         }else {
    123             
    124             //删除单行
    125             [[self.DataArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
    126             [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
    127         }
    128     }else if (editingStyle == UITableViewCellEditingStyleInsert){
    129         //添加数据
    130         //添加一行
    131         [[self.DataArray objectAtIndex:indexPath.section] insertObject:@"Ager" atIndex:indexPath.row];
    132         [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
    133     }
    134 }
    135 
    136 
    137 #pragma mark --- cell 移动 ---
    138 
    139 
    140 /**
    141  *  移动行
    142  *
    143  */
    144 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    145     
    146     //先存储要移动的数据
    147     NSString *str = [[self.DataArray objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
    148     //删除原数据
    149     [[self.DataArray objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    150     //在要移动到地方添加数据
    151     [[self.DataArray objectAtIndex:destinationIndexPath.section] insertObject:str atIndex:destinationIndexPath.row];
    152     [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
    153 }
    154 
    155 
    156 /**
    157  *  限定移动范围
    158  */
    159 - (NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
    160     if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
    161         return proposedDestinationIndexPath;
    162     }else {
    163         return sourceIndexPath;
    164     }
    165 }
    166 
    167 
    168 #pragma mark --- 添加按钮的方法实现 ---
    169 
    170 
    171 /**
    172  *  点击删除按钮
    173  */
    174 - (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    175     
    176     style = UITableViewCellEditingStyleDelete;
    177     [super setEditing:editing animated:animated];
    178     //关联 tableView
    179     [self.rootView.tableView setEditing:editing animated:animated];
    180     self.editButtonItem.title = editing ? @"完成":@"删除";
    181 }
    182 
    183 
    184 /**
    185  *  点击添加按钮
    186  */
    187 - (void)insertAction:(UIBarButtonItem *)sender{
    188     style = UITableViewCellEditingStyleInsert;
    189     addFlag = !addFlag;
    190     [self.rootView.tableView setEditing:addFlag animated:YES];
    191     sender.title = addFlag ? @"完成":@"添加";
    192 }
    193 
    194 - (void)didReceiveMemoryWarning {
    195     [super didReceiveMemoryWarning];
    196     // Dispose of any resources that can be recreated.
    197 }
    198 
    199 /*
    200 #pragma mark - Navigation
    201 
    202 // In a storyboard-based application, you will often want to do a little preparation before navigation
    203 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    204     // Get the new view controller using [segue destinationViewController].
    205     // Pass the selected object to the new view controller.
    206 }
    207 */
    208 
    209 @end
  • 相关阅读:
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Bound mismatch: The typae CertificateDirectory is not a valid substitute for the bounded parameter <M extends Serializable>
    Duplicate property mapping of contactPhone found in
  • 原文地址:https://www.cnblogs.com/Ager/p/4912474.html
Copyright © 2011-2022 走看看