插入删除单元格

来源:互联网 发布:淘宝开店卖红酒挣钱么 编辑:程序博客网 时间: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


3,结果如下面图(2),图(3)所示:

                         

(2)(3)

备注:只是很奇怪的在图(3)的add那块,应该显示+号,结果还是显示-号图标,可能系统原因吧。



0 0
原创粉丝点击