这算是前面移动单元格的升级版,原理还是一样的,,时序图如下图(1),仍是需要判断表视图是否处于编辑状态->进行单元格编辑图标的设置/实现删除或插入处理,接下来就是解析步骤拉;
(1)
1,像view Controller中拖入navigationItem,TableView和textField,都设置为输出口,分别命名为navgationItem,tableview,txtField。而且,在textField的属性检查器中将Font属性设置为System20.0,将Placeholder设置为:Add。。。,将Border Style设置为无边框样式。
2,代码如下:
(1),.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UINavigationItem *navgationItem;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UITextField *txtFiled;
@property(strong,nonatomic) NSMutableArray *listTeams;
@end
(2),.m文件中:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize listTeams=_listTeams;
- (void)viewDidLoad
{
[super viewDidLoad];
//将编辑按钮设置为导航栏右边的按钮,编辑按钮是已经定义好的,只是通过这个语句来获取
self.navgationItem.rightBarButtonItem=self.editButtonItem;
self.navgationItem.title=@"单元格插入与删除";
//设置单元格文本框
self.txtFiled.hidden=YES;
self.txtFiled.delegate=self;
//将当前视图控制器分配给表视图的委托和数据源
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.listTeams=[[NSMutableArray alloc]initWithObjects:@"黑龙江",@"吉林",@"辽宁", nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//点击编辑按钮时,就会调用如下方法:(UIViewController生命周期方法,用于响应视图编辑的状态变化)
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
if(editing){
//表示图处于处于编辑状态时,文本框就显示出来,否则隐藏
self.txtFiled.hidden=NO;
}else{
self.txtFiled.hidden=YES;
}
}
//下面时数据源协议方法:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//加1是为插入准备一个空的单元格
return [self.listTeams count]+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=(UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//判断是否为普通单元格
BOOL b_addCell=(indexPath.row==self.listTeams.count);
//如果是要插入的单元格,则在该单元格的内容视图中添加文本框
if(!b_addCell){
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[self.listTeams objectAtIndex:indexPath.row];
}else{
self.txtFiled.frame=CGRectMake(10, 0, 300, 44);
self.txtFiled.text=@"";
[cell.contentView addSubview:self.txtFiled];
}
return cell;
}
//下面是实现删除或者插入处理:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if(editingStyle==UITableViewCellEditingStyleDelete){
//删除
[self.listTeams removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//重新加载视图数据
[self.tableView reloadData];
}else if(editingStyle==UITableViewCellEditingStyleInsert){
//添加
[self.listTeams insertObject:self.txtFiled.text atIndex:[self.listTeams count]];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
}
}
//设定单元格是否能在选择时处于高亮状态,该方法返回NO,就能选择最后一个单元格
-(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;
}
//放弃第一响应者以关闭键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
//避免键盘遮挡文本框
-(void)textFieldDidBeginEditing:(UITextField *)textField{
//这行代码返回textFiled所在的单元格
UITableViewCell *cell=(UITableViewCell *)[[textField superview]superview];
[self.tableView setContentOffset:CGPointMake(0.0, cell.frame.origin.y) animated:YES];
}
@end
(2) (3)
备注:只是很奇怪的在图(3)的add那块,应该显示+号,结果还是显示-号图标,可能系统原因吧。