zoukankan      html  css  js  c++  java
  • 【转】 UITableViewCell的标记、移动、删除、插入

    原文: http://blog.csdn.net/duxinfeng2010/article/details/7725897

            这篇文章是建立在 

    代码实现 UITableView与UITableViewCell基础上进行修改,用不上的代码我注释调,部分不明白可以看看上篇博客;实现的功能是对UITableViewCell的标记、移动、删除、插入;


    1通过修改cell的accessoryType属性来实现,首先,在ViewDidLoad中[tableView setEditing:NO animated:YES];表示把单元格可编辑状态这只为NO
    1. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
    2. {  
    3.     UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];  
    4.     if (cellView.accessoryType == UITableViewCellAccessoryNone) {  
    5.         cellView.accessoryType=UITableViewCellAccessoryCheckmark;  
    6.     }  
    7.     else {  
    8.         cellView.accessoryType = UITableViewCellAccessoryNone;  
    9.         [tableView deselectRowAtIndexPath:indexPath animated:YES];  
    10.     }  
    11. }  

     当我们选中单元格的时候,调用此函数,首先是indexPath检测选中了哪一行,if判断当前单元格是否被标记,也就是当前单元格风格,是否为UITableViewCellAccessoryCheckmark风格,如果是,则换成UITableViewCellAccessoryNone(不被标记风格)风格,以下accessoryType四个风格属性
     UITableViewCellAccessoryCheckmark                 UITableViewCellAccessoryDetailDisclosureButton
            

    UITableViewCellAccessoryDisclosureIndicator   UITableViewCellAccessoryNone
         

    2.移动  
    实现移动单元格就需要把单元格的编辑属性设置为YES,[tableView setEditing:YES animated:YES];
    1. //返回YES,表示支持单元格的移动  
    2. -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath  
    3. {  
    4.     return YES;  
    5. }  
    1. //单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格  
    2. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
    3. {  
    4.     return UITableViewCellEditingStyleInsert;  
    5. }  
    三种风格的分别是

    UITableViewCellEditingStyleDelete                                                UITableViewCellEditingStyleInsert

      


    UITableViewCellEditingStyleNone


    实现移动的方法
    1. -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath  
    2. {  
    3. //    需要的移动行  
    4.     NSInteger fromRow = [sourceIndexPath row];  
    5. //    获取移动某处的位置  
    6.     NSInteger toRow = [destinationIndexPath row];  
    7. //    从数组中读取需要移动行的数据  
    8.     id object = [self.listData objectAtIndex:fromRow];  
    9. //    在数组中移动需要移动的行的数据  
    10.     [self.listData removeObjectAtIndex:fromRow];  
    11. //    把需要移动的单元格数据在数组中,移动到想要移动的数据前面  
    12.     [self.listData insertObject:object atIndex:toRow];    
    13. }  

    单元格的移动是选中单元格行后面三条横线才可以实现移动的

      
    3.删除
    首先是判断(UITableViewCellEditingStyle)editingStyle,所以
    1. //单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格  
    2. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
    3. {  
    4.     return UITableViewCellEditingStyleDelete;  
    5. }  

    1. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
    2. {  
    3.     if (editingStyle==UITableViewCellEditingStyleDelete) {  
    4. //        获取选中删除行索引值  
    5.         NSInteger row = [indexPath row];  
    6. //        通过获取的索引值删除数组中的值  
    7.         [self.listData removeObjectAtIndex:row];  
    8. //        删除单元格的某一行时,在用动画效果实现删除过程  
    9.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
    10.     }  
    11. }  

    删除了张四 效果图:
        
    4.添加
    实现方法和删除方法相同,首先还是返回单元格编辑风格
    1. //单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格  
    2. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
    3. {  
    4.     return UITableViewCellEditingStyleInsert;  
    5. }  

    为了显示效果明显,在.h文件中声明一个变量i
    1. #import <UIKit/UIKit.h>  
    2.   
    3. @interface STViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>  
    4. {  
    5.  NSInteger i;  
    6. }  
    7. @property(strong,nonatomic) NSMutableArray *listData;  
    8. @property(strong,nonatomic)UITableView *tableView;  
    9. @property(strong,nonatomic)UITableViewCell *tableViewCell;  
    10.   
    11. @end  

    1. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
    2. {  
    3.     if (editingStyle==UITableViewCellEditingStyleDelete) {  
    4. //        获取选中删除行索引值  
    5.         NSInteger row = [indexPath row];  
    6. //        通过获取的索引值删除数组中的值  
    7.         [self.listData removeObjectAtIndex:row];  
    8. //        删除单元格的某一行时,在用动画效果实现删除过程  
    9.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
    10.     }  
    11.     if(editingStyle==UITableViewCellEditingStyleInsert)  
    12.     {  
    13.          
    14.         i=i+1;  
    15.         NSInteger row = [indexPath row];  
    16.         NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath, nil];  
    17.         NSString *mes = [NSString stringWithFormat:@"添加的第%d行",i];  
    18. //        添加单元行的设置的标题  
    19.         [self.listData insertObject:mes atIndex:row];  
    20.         [tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationRight];  
    21.     }  
    22. }  

    运行效果图:
         

    在删除和添加单元格的用到UITableViewRowAnimation动画效果,它还有其他几种效果,在此不做测试

    UITableViewRowAnimationAutomatic      UITableViewRowAnimationTop 

    UITableViewRowAnimationBottom          UITableViewRowAnimationLeft

    UITableViewRowAnimationRight             UITableViewRowAnimationMiddle

    UITableViewRowAnimationFade              UITableViewRowAnimationNone


     
     
    本人补充 :

    //设置进入编辑状态时,Cell不会缩进

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

    {

    return NO;

    }

     

    //使Cell显示移动按钮       

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

    //在下面方法中添加 cell.showsReorderControl =YES;      

     
  • 相关阅读:
    DB2数据库查询过程(Query Processing)复合索引的匹配索引扫描(Matching Index Scans with Composite Indexs)
    DB2数据库查询过程(Query Processing)多索引访问(Multiple Index Access)
    Ubuntu 12.04(32位)安装Oracle 11g(32位)全过程以及几乎所有问题的解决办法
    消费者关注的 Win8 问题汇总(上)
    Surface Pro即将降临 上手体验先睹为快
    简于形 精于心 – 索尼 Duo 11上手体验
    消费者关注的 Win8 问题汇总(中)
    DPI 设置过大该如何还原?
    Ubuntu触摸板失灵
    Ubuntu下安装、配置git和gitflow
  • 原文地址:https://www.cnblogs.com/A--G/p/4873753.html
Copyright © 2011-2022 走看看