码迷,mamicode.com
首页 > 编程语言 > 详细

开发进阶16_删除和排序

时间:2014-10-28 00:20:06      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:des   style   io   color   ar   for   sp   strong   数据   

#import "ViewController.h"

#import "Person.h"

 

@interface ViewController () <UITableViewDataSource,UITableViewDelegate>

{

    NSMutableArray *_persons;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //初始化数据

    _persons = [NSMutableArray array];

    for (int i = 0; i < 30; i++) {

        NSString *name = [NSString stringWithFormat:@"Person--%d",i];

        NSString *phone = [NSString stringWithFormat:@"%d",1000000 + arc4random_uniform(1000000)];

        Person *p = [Person personWithName:name andPhone:phone];

        [_persons addObject:p];

    }

}

 

#pragma  mark - 数据源方法

#pragma mark 返回每组数据行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return _persons.count;

}

 

#pragma mark 返回每组数据Cell

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

{

    static NSString *ID = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if(cell == nil){

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];

    }

    Person *p = _persons[indexPath.row];

    cell.textLabel.text = p.name;

    cell.detailTextLabel.text = p.phone;

    return cell;

}

 

#pragma mark - 代理方法

#pragma mark 当用户提交了一个编辑操作就会调用

 

//只要实现这个方法,就会默认添加滑动删除功能

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    //1.删除模型数据

    [_persons removeObjectAtIndex:indexPath.row];

    

    //2.刷新表格

    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

}

 

#pragma mark 当移动某一行cell就会调用

 

//只要添加这个方法,就会默认添加排序功能

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

    //1.取出要拖动的模型数据

    Person *p = _persons[sourceIndexPath.row];

    

    //2.删除之前行的数据

    [_persons removeObjectAtIndex:sourceIndexPath.row];

    

    //3.插入数据到新的位置

    [_persons insertObject:p atIndex:destinationIndexPath.row];

    

    //交换两个元素的位置

    //[_persons exchangeObjectAtIndex:原位置 withObjectAtIndex:目标位置];

    

}

 

#pragma mark 删除

- (IBAction)remove:(id)sender {

    //1.进入编辑模式

    BOOL result = !self.tableView.editing;

    [self.tableView setEditing:result animated:YES];

    

}

@end

开发进阶16_删除和排序

标签:des   style   io   color   ar   for   sp   strong   数据   

原文地址:http://www.cnblogs.com/yaofch107/p/4055455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!