要进行单元格的修改,首先我们要删除原来的视图并从对象库中拖曳一个Navigation Controller视图(导航控制器)到设计页面,添加Navigation Controller视图的同时,会自动添加一个表视图控制器(Root View Controller),这是Navigation Controller视图的根视图控制器。同时勾选Navigation Controller视图中的Is Initial View Controller.(设置初始视图)。如何所示
然后将ViewController的父类从UIViewController修改为TableViewController,然后设置Root View Controller的class 为ViewController,再从对象库中拖曳一个Text Field到Root View Controller中,并命名为txtField,并定义输出口,如下图
下面就是进行代码的编写:
/*********************************************ViewController.h*********************************************/
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
@property (strong, nonatomic) IBOutlet UITextField *txtField;
@property (strong,nonatomic) NSMutableArray * listTeams;
@end
/*********************************************ViewController.m*********************************************/
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航栏
self.navigationItem.rightBarButtonItem = self.editButtonItem;//将编辑栏设为导航栏右边的按钮,当编辑按钮时,会调用setEditing:animated
self.navigationItem.title = @"单元格插入和删除";//设置导航栏标题
//设置单元格文本
self.txtField.hidden = YES;//默认文本框隐藏
self.txtField.delegate = 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.
}
#pragma mark
//响应视图编辑状态的变化
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
if(editing)
{
self.txtField.hidden = NO;
}
else
{
self.txtField.hidden = YES;
}
}
//返回数据的长度,当前返回的为listTeams长度加1,为add按钮准备一个单元格
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listTeams count] + 1;
}
//设置单元格
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"Cell";
BOOL b_addCell = (indexPath.row == self.listTeams.count);
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(!b_addCell)
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [self.listTeams objectAtIndex:indexPath.row];
}
else
{
self.txtField.frame = CGRectMake(10, 0, 300, 44);
self.txtField.borderStyle = UITextBorderStyleNone;
self.txtField.placeholder = @"Add...";
self.txtField.text =@"";
[cell.contentView addSubview:self.txtField];
}
return cell;
}
//单元格编辑图标的设定
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%li",(long)indexPath.row);
if(indexPath.row == [self.listTeams count])
{
return UITableViewCellEditingStyleInsert;
}
else
{
return UITableViewCellEditingStyleDelete;
}
}
//实现插入和删除操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
NSArray * indexPaths = [NSArray arrayWithObject:indexPath];
if(editingStyle == UITableViewCellEditingStyleDelete)
{
[self.listTeams removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
[self.listTeams insertObject:self.txtField.text atIndex:[self.listTeams count]];
[self.tableView insertRowsAtIndexPaths:indexPaths 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;
}
@end
运行效果如下: