zoukankan      html  css  js  c++  java
  • UITableView的排序与删除

    UITableView有一个强大的编辑模式(editing mode),在编辑模式中可实现删除,插入,多选,排序等功能。使用的方法多很简单。以下介绍删除和排序功能:

    首先查看SDK提供的API:

    复制代码
    // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    
    // Moving/reordering
    // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
    
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPat
    
     // Data manipulation - reorder / moving support
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    复制代码

    创建UITableView后简单的实现以上4个代理就可以实现删除和排序的功能。当然,你必须实现 UItableViewDataSource 穿件Cell的两个代理。以下是完整代码:

    @interface ViewController () <UITableViewDataSource, UITableViewDelegate>
    {
        UITableView *_tableView;
    }
    @end
    
    @implementation ViewController
    
    - (void)dealloc
    {
        _tableView.delegate = nil;
        _tableView.dataSource = nil;
        [_tableView release],_tableView = nil;
    
        [super dealloc];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        UIBarButtonItem *navItem = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonSystemItemEdit target:self action:@selector(editBUttonOnClicked)];
        self.navigationItem.rightBarButtonItem = navItem;
      [navItem release];
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        
    }
    
    - (void)editBUttonOnClicked
    {
        [_tableView setEditing:!_tableView.editing animated:YES];
        
        if (!_tableView.editing) {
            UIBarButtonItem *barButtonItem = self.navigationItem.rightBarButtonItem;
            barButtonItem.title = @"Edit";
        }else {
            UIBarButtonItem *barButtonItem = self.navigationItem.rightBarButtonItem;
            barButtonItem.title = @"Done";
        }
    }
        
    #pragma mark - UItableViewDataSource
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 10;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifity = @"cellIdentifity";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifity];
        if (!cell) {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifity]autorelease];
        }
        
        cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
        return cell;
    }
    
    // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }
    
    // Moving/reordering
    
    // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES;
    }
    // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPat { } // Data manipulation - reorder / moving support // - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {   //修改对应的数据源   // id object=[[dataSource objectAtIndex:[fromIndexPath row]] retain];   // [dataSource removeObjectAtIndex:[fromIndexPath row]];   // [dataSource insertObject:object atIndex:[toIndexPath row]];   // [object release]; }

    说明:

    1.必须实现 moveRowAtIndexPath 这个代理,才会出现可以拖拽的按钮;更新相关的NSArray数据才可以成功改变数据,

    commitEditingStyle方法中需要reload数据。

    2.根据需要设置cell是否支持排序和删除功能。

  • 相关阅读:
    Return Largest Numbers in Arrays-freecodecamp算法题目
    Title Case a Sentence-freecodecamp算法题目
    Find the Longest Word in a String-freecodecamp算法题目
    Check for Palindromes-freecodecamp算法题目
    Factorialize a Number-freecodecamp算法题目
    Reverse a String-freecodecamp算法题目
    js拖动div
    Jquery $.ajax()方法详解
    jQuery中$.each()方法的使用
    echarts的pie图中,各区块颜色的调整
  • 原文地址:https://www.cnblogs.com/ranger-cc/p/3520218.html
Copyright © 2011-2022 走看看