因为TableView是屏幕的一部分,所以ViewControll不再继承UITableViewControl,继而必须加入tableView的委托协议<UITableViewDelegate,
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *students;
@property (nonatomic, retain) UIButton *editButton;
@end
@implementation ViewController
-(void)dealloc
{
[_tableView release];
[_students release];
[_editButton release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 设置背景色
self.view.backgroundColor = [UIColor grayColor];
// 准备初始数据
self.students = [NSMutableArray arrayWithObjects:@"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:self action:@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:self action:@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
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 基本table view方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.students count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 标识符
static NSString *identifier = @"StudentCell";
// 重用及创建cell
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
// 获得当前student对象
NSString *studentName = [self.students objectAtIndex:indexPath.row];
// 设置cell的属性
cell.textLabel.text = studentName;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark - 控制table view编辑的方法
// 是否允许编辑(删除)
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// 执行编辑(删除)
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 实现删除功能
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// 删除原始数据
[self.students removeObjectAtIndex:indexPath.row];
// 删除单元格
//[self.tableView reloadData];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
// 是否允许移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// 移动到的最终目标indexpath
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
return proposedDestinationIndexPath;
}
// 执行移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(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:sourceIndexPath toIndexPath: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.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
// 切换编辑态
-(void)toggleEdit
{
// 判断当前是否在编辑态
if (self.tableView.editing)
{
[self.tableView setEditing:NO animated:YES];
[self.editButton setTitle:@"Edit" forState:UIControlStateNormal];
}
else
{
[self.tableView setEditing:YES animated:YES];
[self.editButton setTitle:@"Done" forState:UIControlStateNormal];
}
}
@end
// 删除一个section的方法
deleteSections:withRowAnimation: