插入删除单元格
来源:互联网 发布:淘宝开店卖红酒挣钱么 编辑:程序博客网 时间:2024/06/15 00:09
这算是前面移动单元格的升级版,原理还是一样的,,时序图如下图(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) IBOutletUINavigationItem *navgationItem;
@property (strong,nonatomic) IBOutletUITableView *tableView;
@property (strong,nonatomic) IBOutletUITextField *txtFiled;
@property(strong,nonatomic)NSMutableArray *listTeams;
@end
(2),.m文件中:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize listTeams=_listTeams;
- (void)viewDidLoad
{
[superviewDidLoad];
//将编辑按钮设置为导航栏右边的按钮,编辑按钮是已经定义好的,只是通过这个语句来获取
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=[[NSMutableArrayalloc]initWithObjects:@"黑龙江",@"吉林",@"辽宁",nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//点击编辑按钮时,就会调用如下方法:(UIViewController生命周期方法,用于响应视图编辑的状态变化)
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editinganimated:animated];
[self.tableViewsetEditing:editing animated:YES];
if(editing){
//表示图处于处于编辑状态时,文本框就显示出来,否则隐藏
self.txtFiled.hidden=NO;
}else{
self.txtFiled.hidden=YES;
}
}
//下面时数据源协议方法:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//加1是为插入准备一个空的单元格
return [self.listTeamscount]+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=(UITableViewCell *) [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}
//判断是否为普通单元格
BOOL b_addCell=(indexPath.row==self.listTeams.count);
//如果是要插入的单元格,则在该单元格的内容视图中添加文本框
if(!b_addCell){
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[self.listTeamsobjectAtIndex: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.listTeamsremoveObjectAtIndex:indexPath.row];
[self.tableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//重新加载视图数据
[self.tableViewreloadData];
}elseif(editingStyle==UITableViewCellEditingStyleInsert){
//添加
[self.listTeamsinsertObject:self.txtFiled.textatIndex:[self.listTeamscount]];
[self.tableViewinsertRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableViewreloadData];
}
}
//设定单元格是否能在选择时处于高亮状态,该方法返回NO,就能选择最后一个单元格
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row==[self.listTeamscount]){
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 *)[[textFieldsuperview]superview];
[self.tableViewsetContentOffset:CGPointMake(0.0, cell.frame.origin.y)animated:YES];
}
@end
(2)(3)
备注:只是很奇怪的在图(3)的add那块,应该显示+号,结果还是显示-号图标,可能系统原因吧。
- 插入删除单元格
- 实现单元格的插入与删除
- 插入与删除Excel表格的单元格、行和列
- 单元格删除
- 关于单元格插入图片
- 动态添加删除单元格
- js删除单元格
- 自定义删除单元格
- Excel 单元格中插入图片
- Excel 单元格中插入附件
- 删除Excel单元格中的换行
- UITableView删除移动单元格思路
- jxl操作excel 单元格样式,合并单元格,插入图片,超链接
- jxl操作excel 单元格样式,合并单元格,插入图片,超链接
- VC 创建WORD ,合并单元格,插入图片.
- Excel中插入多行单元格
- C# Excel单元格合并及图片插入
- Word批量给每个单元格插入书签
- kaldi上使用gpu以及如何安装cuda
- log4j properties配置
- cocos2dx 坐标系锚点总结
- 单向链表的C++实现
- iOS开发 64位与32位的区别
- 插入删除单元格
- 鼠标滑过,实现下拉菜单弹出的示例代码
- Unix&Linux
- 6.6.1 F# 中函数调用的类型推断
- selenium webdriver无法解析为类型的问题
- \r \n有什么区别
- ios真机调试
- 《Linux设备驱动程序》——硬件通信
- Remove Duplicates from Sorted Array(去掉重复的元素)leetcode