UITableView的编辑操作

来源:互联网 发布:淘宝买家虚假交易提醒 编辑:程序博客网 时间:2024/05/16 19:11
因为TableView是屏幕的一部分,所以ViewControll不再继承UITableViewControl,继而必须加入tableView的委托协议<UITableViewDelegate,UITableViewDataSource>

具体实现代码如下:

#import "ViewController.h"


@interface ViewController()


@property (nonatomic,retain) UITableView *tableView;

@property (nonatomic,retain) NSMutableArray *students;

@property (nonatomic,retain) UIButton *editButton;


@end


@implementationViewController


-(void)dealloc

{

   [_tableView release];

   [_students release];

   [_editButton release];

    [super dealloc];

}


-(void)viewDidLoad

{

   [superviewDidLoad];

// Do any additional setup after loading the view, typically from anib.

    

   // 设置背景色

   self.view.backgroundColor = [UIColor grayColor];

    

   // 准备初始数据

   self.students =[NSMutableArrayarrayWithObjects:@"Tom", @"Alan", @"Peter", nil];

    

   // 添加按钮

   UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    addButton.frame = CGRectMake(230.0, 10.0, 80.0, 50.0);

   [addButton setTitle:@"Add"forState:UIControlStateNormal];

   [addButton addTarget:selfaction:@selector(addStudent) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:addButton];

    

   // 编辑按钮

   UIButton *editButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    editButton.frame = CGRectMake(10.0, 10.0, 80.0, 50.0);

   [editButton setTitle:@"Edit" forState:UIControlStateNormal];

   [editButton addTarget:selfaction:@selector(toggleEdit) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:editButton];

    self.editButton = editButton;

    

   // 创建UITableView实例

   UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0,70.0, 320.0,390.0) style:UITableViewStylePlain];

    

   // 加到父级视图上

    [self.view addSubview:tableView];

    

   // 设置self.tableView属性

    self.tableView = tableView;

    [tableView release];

    

   // 设置self.tableView的两个代理

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

}


-(void)didReceiveMemoryWarning

{

   [superdidReceiveMemoryWarning];

   // Dispose of any resources that can berecreated.

}


#pragma mark - 基本tableview方法


-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

    return 1;

}


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

{

    return [self.students count];

}


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

{

   // 标识符

    static NSString *identifier = @"StudentCell";

    

   // 重用及创建cell

   UITableViewCell *cell = [self.tableViewdequeueReusableCellWithIdentifier:identifier];

    if (!cell)

    {

       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier]autorelease];

    }

    

   // 获得当前student对象

    NSString *studentName = [self.students objectAtIndex:indexPath.row];

    

   // 设置cell的属性

    cell.textLabel.text = studentName;

   cell.selectionStyle=UITableViewCellSelectionStyleNone;

    

    return cell;

}


#pragma mark - 控制tableview编辑的方法


//是否允许编辑(删除)

-(BOOL)tableView:(UITableView *)tableViewcanEditRowAtIndexPath:(NSIndexPath *)indexPath

{

   return YES;

}


//执行编辑(删除)

-(void)tableView:(UITableView *)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath*)indexPath

{

   // 实现删除功能

   if(editingStyle ==UITableViewCellEditingStyleDelete)

    {

       // 删除原始数据

       [self.students removeObjectAtIndex:indexPath.row];

       

       //删除单元格

       //[self.tableViewreloadData];

       [self.tableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

    }

}


//是否允许移动

-(BOOL)tableView:(UITableView *)tableViewcanMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

   return YES;

}


// 移动到的最终目标indexpath

-(NSIndexPath*)tableView:(UITableView*)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPathtoProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath

{

    return proposedDestinationIndexPath;

}


//执行移动

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

{

   // 删除原数据

    NSString *movingStudent = [[self.students objectAtIndex:sourceIndexPath.row] retain];

    [self.students removeObjectAtIndex:sourceIndexPath.row];

   // 插入新位置

    [self.students insertObject:movingStudent atIndex:destinationIndexPath.row];

    [movingStudent release];

    

   // 移动单元格

    [self.tableView moveRowAtIndexPath:sourceIndexPathtoIndexPath:destinationIndexPath];

}


#pragma mark - 自定义方法


//添加记录

-(void)addStudent

{

   // 在数据里添加对象

    NSInteger newRowNumber = [self.students count];

    [self.students addObject:[NSString stringWithFormat:@"Student #%d", newRowNumber]];

    

   // 添加单元格

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowNumber inSection:0];

   [self.tableViewinsertRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

}


//切换编辑态

-(void)toggleEdit

{

   // 判断当前是否在编辑态

    if (self.tableView.editing)

    {

       [self.tableView setEditing:NOanimated:YES];

       [self.editButton setTitle:@"Edit" forState:UIControlStateNormal];

    }

    else

    {

       [self.tableView setEditing:YESanimated:YES];

       [self.editButton setTitle:@"Done" forState:UIControlStateNormal];

    }

}


@end


// 删除一个section的方法

deleteSections:withRowAnimation:

0 0
原创粉丝点击