<pre name="code" class="objc">ViewController.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property(weak,nonatomic) IBOutlet UINavigationItem *navgationItem;
@property(weak,nonatomic) IBOutlet UITableView *tableView;
@property(strong,nonatomic) IBOutlet UITextField *txtField;
@property(nonatomic,strong) NSMutableArray *listTeams;
@end
ViewController.m文件
//
// ViewController.m
// 表视图的增删除
//
// Created by 刘薇薇 on 16/6/10.
// Copyright (c) 2016年 刘胡来. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void) InitSetting
{
self.navgationItem.rightBarButtonItem = self.editButtonItem;
self.navgationItem.title = @"单元格插入和删除";
self.txtField.hidden = YES;
self.txtField.delegate = self;
//将当前视图控制器分配给表视图的委托和数据源
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.listTeams = [[NSMutableArray alloc] initWithObjects:@"黑龙江", @"吉林", @"辽宁", nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self InitSetting];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -- UIViewController生命周期方法,用于响应视图编辑状态变化
//重写系统导栏可编辑按钮
-(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;
}
}
//重写表格视图的相关接口
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listTeams count] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(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.text = @"";
[cell.contentView addSubview:self.txtField]; //将文本框添加到表格视图中
}
return cell;
}
#pragma mark --UITableViewDelegate协议方法--可编辑接口
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row ==[self.listTeams count]) {
return UITableViewCellEditingStyleInsert;
}
else
{
return UITableViewCellEditingStyleDelete;
}
}
//获取可编辑事件
- (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.txtField.text atIndex:[self.listTeams count]];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]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;
}
#pragma mark -- UITextFieldDelegate委托方法,关闭键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#pragma mark -- UITextFieldDelegate委托方法,避免键盘遮挡文本框
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
[self.tableView setContentOffset:CGPointMake(0.0,cell.frame.origin.y) animated:YES];
}
@end