@interface ViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *textField;
@property (nonatomic, strong) NSMutableArray * listTeams;
@end
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航栏
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.title = @"单元格插入删除";
//设置单元个文本框
self.textField.hidden = YES;
self.textField.delegate = self;
self.listTeams = [NSMutableArray arrayWithObjects:@"中国",@"河北",@"北京" ,nil];
}
#pragma mark UIViewController 生命周期的方法,用于响应视图编辑状态的变化
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
if (editing) {
self.textField.hidden = NO;
} else {
self.textField.hidden = YES;
}
}
#pragma mark 实现UITableViewDatasource的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.listTeams.count+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ident = @"cell";
BOOL b_addCell = (indexPath.row==self.listTeams.count);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
}
if (!b_addCell) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [self.listTeams objectAtIndex:indexPath.row];
} else {
self.textField.frame = CGRectMake(10, 0, 300, 33);
self.textField.borderStyle = UITextBorderStyleNone;
self.textField.placeholder = @"add...";
self.textField.text = @"";
[cell.contentView addSubview:self.textField];
}
return cell;
}
//用于单元格编辑图标的设定
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone; //可移动单元格
// if (indexPath.row == [self.listTeams count]) {
// return UITableViewCellEditingStyleInsert; //插入
// } else {
// return UITableViewCellEditingStyleDelete; // 删除
// }
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES; //表示可以移动单元格
}
//当拖动排序控件时会出发 进行排序
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSString *stringToMove = [self.listTeams objectAtIndex:sourceIndexPath.row];
[self.listTeams removeObjectAtIndex:sourceIndexPath.row];
[self.listTeams insertObject:stringToMove atIndex:destinationIndexPath.row];
}
//用于实现删除或插入处理
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *indextPaths = [NSArray arrayWithObject:indexPath];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.listTeams removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:indextPaths withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
[self.listTeams insertObject:self.textField.text atIndex:[self.listTeams count]];
[self.tableView insertRowsAtIndexPaths:indextPaths withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView reloadData];
}
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == [self.listTeams count]) {
return NO;
} else {
return YES;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}