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的两个代理。以下是完整代码:

    View Code
    @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 这个代理,才会出现可以拖拽的按钮;

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

  • 相关阅读:
    从零开始通过webhooks实现前端自动化
    使用rem配置PC端自适应大屏
    Nuxt内导航栏的两种实现方式
    VueX中直接修改数据报错,修改一维数组,二维数组,报错的原因
    在mpvue或者Vue中使用VUEX
    小程序框架MpVue踩坑日记(二)
    小程序mpvue中动态切换echarts图表
    小程序踩坑之不同屏幕下动态改变translate值
    Koa2+MySQL+VUE+ElementIUI搭建简单的后台管理小系统
    小程序框架MpVue踩坑日记(一)
  • 原文地址:https://www.cnblogs.com/zeejun/p/2744085.html
Copyright © 2011-2022 走看看