UITableView 排序、删除

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

UITableView 排序、删除

 

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

你可能感兴趣的:(UITableView)