zoukankan      html  css  js  c++  java
  • UITableView的编辑模式

    好像所有的计划都没有得到完好的实行,所以我还傻傻在原地,总有意外,这是借口.    -----题记

    回归正题,学习是万能的...fighting!fighting!fighting!

     UITableView有两种模式

     1、常规的选择模式(Selection Mode)

         相信大家对此不会陌生,这里便不介绍

     2、编辑模式(Editing mode)

         在编辑模式中可实现插入、删除、多选、重排序等。

        (1)那如何设置编辑模式?

              通过直接设置UITableView的editing属性或向其发送setEditing:animated消息

              self.tableView.editing =YES;或[self.tableView setEditing:YES animated:YES];

             注:UIViewController 本身也有editing属性和setEditing:animated:方法,在当前视图控制器由导

               航栏中包含editButtonItem时,若UIViewController的editing为NO,则显示为"Edit",若

               editing为YES,则显示为"Done".可利用此按钮在设置UIViewController的editing状态时同时设置

               tableView的编辑状态。

               -(void)viewDidLoad

               {

                   [super viewDidLoad];

                   self.navigationItem.rightBarButtonItem = self.editButtonItem;

                }

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

               {

                    [super setEditing:editing animated:animated];

                    [self.tableView setEditing:editing animated:animated];

                }

                UITableView接收到setEditing:animated:消息时,会发送同样的消息到所有可见的cell,设置其

                编辑模式。

             (2)插入和删除操作

             首先,UITableVie通过向DataSource发送:tableView:canEditRowAtIndexPath:消息询问每个

             indexPath是否可编辑,在此方法中对不可以编辑的cell返回NO,可以编辑的cell返回YES;

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

              {

                   if(indexPath.row ==N)

                   {

                        return NO;

                   }

                    return YES;

              } 

              然后,UITableView向其delegate发送tableView:editingStyleForRowAtIndexPath询问

              EditingStyle (删除:UITableViewCellEditingStyleDelete 或

              插入:UITableViewCellEditingStyleInsert),默认为删除操作。

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

              {

                     return UITableViewCellEditingStyleDelete;

                  // return UITableViewCellEditingStyleInsert;

               }

               删除

               

                插入

                

               当点击"Delete"或"加号"按钮时,UITableView 向其DataSource发送

               tableView:commitEditingStyle:ForRowAtIndexPath:(NSIndexPath *)indexPath

              {

                   if(editingStyle ==UITableViewCellEditingStyleDelete)

                   {

                        [dataArray removeObjectAtIndex:indexPath.row];

                        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]

                         withRowAnimation:UITableViewRowAnimationFade];

                    }

                    else if(editingStyle == UITableViewCellEditingStyleInsert)

                    {

                          [dataArray insertObject:"new Item" atIndex:indexPath.row];

                          [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]

                          withRowAnimation:UITableViewRowAnimationFade];

                     }

                  }

                  当需要删除或插入section时,需要调用deleteSections:withRowAnimation 或

                  insertSections:WithRowAnimation

                 (3) 重排序

                     若当前tableView允许重排序,则会在每个cell的右侧出现三条灰色横线的控件,拖动此空间

                     可将cell移动到不同的位置。重排序模式和删除/插入是并行的,即可在显示重排序空间的同

                     时显示删除或插入控件。

                     当tableView的dataSouce实现tableView:moveRowAtIndexPath:toIndexPath:方法后,

                     tableView进入编辑模式后就会在右侧显示“重排序”控件

                      其消息处理流程为:

                      (a) tableView收到setEditing:animated:消息并将同样的消息发送给可见的cell。

                      (b) tableView向其DataSource发送tableView:canMoveRowAtIndexPath:消息,询问每一行

                          是否可显示重排序空间,若为NO,则不显示,若为YES则显示。此方法不实现时默认所有

                          行都可显示重排序控件。这时就会在每一行的右侧显示重排序控件。
                          因为重排序没有使用向delegate发送tableView:editingStyleForRowAtIndexPath:消息

                          询问编辑模式,所以其 与删除、插入控件可同时存在,在一般情况下不应该同时出

                          现,所以应实现了 tableView:editingStyleForRowAtIndexPath:并返回

                          UITableViewCellEditingStyleNone;若不实现

                          tableView:editingStyleForRowAtIndexPath:则会默认使用删除模式,即右侧出现“排

                          序”控件时,左侧会出现”删 除”控件。

                      (c)用户可拖动每行右侧的空间来移动该行的位置。

                      (d)用户拖动某行经过目标行上方时,tableView会向delegate发送

                         tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:(若

                         delegate有实现)消息询问是否可移动到此位置(ProposedIndexPath),若不可移动到

                         此位置则返回一个新的目的indexPath,可以的话直接将ProposedIndexPath返回即可。一

                         般情况下不需实现此方法。

                         -(NSIndexPath *)tableView:(UITableView *)tableView 

                       targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)

                       sourceIndexPath toProposedIndexPath:(NSIndexPath *)

                       proposedDestinationIndexPath

                       {    

                            if (proposedDestinationIndexPath.row == 5)

                            {        

                                  return [NSIndexPath indexPathForRow:8

                                     inSection:0];   

                            }    

                            return proposedDestinationIndexPath;

                        }

                        (e)tableView向其DataSource发送tableView:moveRowAtIndexPath:toIndexPath:消

                               息,在此方法中更改tableView的数据模型,移动里面数据项的位置。

                            - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:

                            (NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)

                            destinationIndexPath

                            { 

                                if(sourceIndexPath == destinationIndexPath)         

                                        return; 

                                id object = [dataArray

                                objectAtIndex:sourceIndexPath.row]; 

                                [dataArray removeObjectAtIndex:sourceIndexPath.row];     

                                [dataArray insertObject:object

                                atIndex:destinationIndexPath.row];

                            }

                       (4)多行选取模式

                          

                          实现类似苹果自带的邮件程序中点击编辑按钮后会出现使用”红勾”多选的效果可

                             通过以下两种途径

                             (a)苹果公共API

                                在iOS5.0中UITableView增加了allowsMultipleSelectionDuringEditing属性和

                                 indexPathsForSelectedRows方 法,allowsMultipleSelectionDuringEditing

                                属性默认为NO,当此值为YES时,UITableView进入编辑模式 不会向dataSource查

                                询editStyle,而会直接每个Cell的左边显示圆形选择框,点击选择该行后圆形

                                框里面为对勾。可使用 indexPathsForSelectedRows方法获取到所有选择行的

                                indexPath。

                             (b)苹果私用API

                                在iOS5之前,苹果并没有提供多行选取的API,但其内部确实实现了,我们可以

                                通过使用私有API实现。

                                在tableView:editingStyleForRowAtIndexPath:方 法中若返回的是 

                                UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert则

                                可以进入 多选模式,效果同allowsMultipleSelectionDuringEditing设为YES时

                                相同。这也是“多选”控件不会与“插入”控 件,”删除”控件同时出现,却可

                                以和”重排序”控件同时存在的原因。

                                获取到选择的行时,同样可以使用私有方法indexPathsForSelectedRows获取,

                                或者使用公开的

                                tableView:didSelectRowAtIndexPath:,tableView:didDeselectRowAtIndexPath                            :方法在选择/取消选择时逐个获取并保存。

                                注:以上两种方式均需保证UITableViewCell的selectionStyle属性不为

                                   UITableViewCellSelectionStyleNone,否则选择后的“红勾”无法显示。

        

           参考:http://linglong117.blog.163.com/blog/static/277145472012103075527791/

    本文转载至:http://blog.sina.com.cn/s/blog_adf4baca0101b286.html

  • 相关阅读:
    nginx
    mysql
    intelij maven
    redis命令大全
    绑定touch事件后click无效,vue项目解决棒法
    新的用法
    img
    vuedragable
    自己总结
    vuex的项目在id中不能运行
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3140170.html
Copyright © 2011-2022 走看看