zoukankan      html  css  js  c++  java
  • UITableView (4): 在TableView中移动cell和Section 从TableView中删除cell和section 添加系统默认刷新控件

    一  .问题:你想用流畅直观的动画来移动和拖拽TableView中的cell和section

    方案:

      用moveSection:toSection:方法把一个Section移动到新位置.

      用moveRowAtIndexPath:toIndexPath:方法把一个cell从当前位置移动到新位置

    例子:

      创建一个TableView并在其中加载3个Section,每个Section有3个cell

    #pragma - mark 初始化数据
    
    - (NSMutableArray *)newSectionWithIndex:(NSUInteger)Index withCellCount:(NSUInteger)cellCount{
        NSMutableArray *result = [[NSMutableArray alloc]init];
        for (NSUInteger counter = 0; counter < cellCount; counter++) {
            [result addObject:[[NSString alloc]initWithFormat:@"Section %lu Cell %lu",Index,counter + 1]];
        }
        return result;
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        //在实例化方法中填充数据
        if (self != nil) {
            _arrayOfSections = [[NSMutableArray alloc]init];
            NSMutableArray *section1 = [self newSectionWithIndex:1 withCellCount:3];
            NSMutableArray *section2 = [self newSectionWithIndex:2 withCellCount:3];
            NSMutableArray *section3 = [self newSectionWithIndex:3 withCellCount:3];
            [_arrayOfSections addObject:section1];
            [_arrayOfSections addObject:section2];
            [_arrayOfSections addObject:section3];
        }
        return self;
    }

    以上功能要实现 ,在AppDelegate中实现

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        ViewController *vc = [[ViewController alloc]initWithNibName:nil bundle:nil];
        self.window.rootViewController = vc
        ;
        return YES;
    }

    然后创建TableView并实现相关协议方法

    #pragma mark - 协议方法
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return _arrayOfSections.count;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [[_arrayOfSections objectAtIndex:section]count];
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *result = nil;
        static NSString *cellIdentifier = @"CellIdentifier";
        result = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (result == nil) {
            result = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        result.textLabel.text = [[_arrayOfSections objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
        return result;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        _myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _myTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        
        [self.view addSubview:_myTableView];
        
        NSTimer *timer = [NSTimer timerWithTimeInterval:2.0f target:self selector:@selector(moveSection1ToSection3) userInfo:nil repeats:NO];
        [timer fire];
    }

    最后实现section 和 cell 的相关移动  ,功能实现代码如下:

    #pragma mark - Section 和 cell 是如何实现移动的
    //section1 移动到section3
    - (void)moveSection1ToSection3{
        //数据操作
        NSMutableArray *section1 = [_arrayOfSections objectAtIndex:0];
        [_arrayOfSections removeObject:section1];
        [_arrayOfSections addObject:section1];
        //TableView移动section
        [_myTableView moveSection:0 toSection:2];
        
        [self moveCellInSection1ToCell2InSection1];
    }
    //同section移动cell
    - (void)moveCellInSection1ToCell2InSection1{
        //数据
        NSMutableArray *section1 = [_arrayOfSections objectAtIndex:0];
        NSString *cell1InSection1 = [section1 objectAtIndex:0];
        [section1 removeObject:cell1InSection1];
        [section1 insertObject:cell1InSection1 atIndex:1];
        //视图
        [_myTableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        
        [self moveCell2InSection1ToCell1InSection2];
    }
    //不同section移动cell
    - (void)moveCell2InSection1ToCell1InSection2{
        NSMutableArray *section1 = [_arrayOfSections objectAtIndex:0];
        NSMutableArray *section2 = [_arrayOfSections objectAtIndex:1];
        NSString *cell2InSection1 = [section1 objectAtIndex:1];
        [section1 removeObject:cell2InSection1];
        [section2 insertObject:cell2InSection1 atIndex:0];
        
        [_myTableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    }

    二.从TableView中删除cell和section

    1.删除section:

      首先删除数据源中的section;

      然后调用TableView的实例方法deleteSection:withRowAnimation: 第一个参数是NSIndexSet类型,可通过indexSetWithIndex: 来实例化 且indexSetWithIndexesInRange:能

    2.删除cell:

      首先删除cell对应的数据;

      deleteRowsAtIndexPaths:withRowAnimation:    

    #pragma mark - 删除cell和section
    //删除section
    - (void)deleteSection1{
        //数据源
        [_arrayOfSections removeObjectAtIndex:0];
        //然后删除TableView的section
        NSIndexSet *sectionToDelete = [NSIndexSet indexSetWithIndex:0];
        [_myTableView deleteSections:sectionToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
        [self deleteCell2InSection3];
    }
    
    //删除cell
    - (void)deleteCell2InSection3{
        NSMutableArray *section3 = [_arrayOfSections objectAtIndex:[_arrayOfSections count]-1];
        [section3 removeObjectAtIndex:1];
        //添加多个indexPath删除多行
        [_myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:1 inSection:2], nil] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

     三. 向TableView添加系统默认刷新控件 UIRefreshController

    //刷新控件
        _refreshControl = [[UIRefreshControl alloc]init];
        [_refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
        [_myTableView addSubview:_refreshControl];
    - (void)handleRefresh:(id)Sender{
        NSLog(@"valueChanged");
        [_refreshControl endRefreshing];
    }

    该刷新简单实现,更多功能再添加..

  • 相关阅读:
    Siege 3.0 正式版发布,压力测试工具
    Pomm 1.1.2 发布,专为 PG 设计的 ORM 框架
    Whonix 0.5.6 发布,匿名通用操作系统
    国内开源 java cms,Jspxcms 2.0 发布
    EZNamespaceExtensions.Net v2013增加对上下文菜单、缩略图、图标、属性表的支持
    GNU Guile 2.0.9 发布,Scheme 实现
    jdao 1.0.4 发布 轻量级的orm工具包
    OpenSearchServer 1.4 RC4 发布
    Percona Server for MySQL 5.5.3030.2
    Samba 4.0.5 发布
  • 原文地址:https://www.cnblogs.com/safiri/p/4047775.html
Copyright © 2011-2022 走看看