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是否支持排序和删除功能。

  • 相关阅读:
    微信公众号 发送客服消息
    juqery 点击谁获取他的值,赋给input标签
    微信执行退出页面,直接回到微信对话窗口
    微信jssdk上传图片,一张一张的上传 和 一次性传好几张
    juqery 判断所有input 不能为空 判断只能为数字 判断身份证号:18位和15位 判断是否银行卡号
    php foreach
    现在越来越喜欢用ajax传值了,这样能让网站的体验性很好,今天就总结了一下常用的
    有时候不用explode截取字符串了,可以用用substr()
    ztree 文件夹类型的 树状图
    POJ 1065 Wooden Sticks
  • 原文地址:https://www.cnblogs.com/ranger-cc/p/3520218.html
Copyright © 2011-2022 走看看