移动单元格

整的很崩溃的一个程序:

1,在原来View Controller基础上拖入一个table view 和navigationItem,且将navigationItem的尺度检查器中的y值设置为20(只是为了避免navigationItem将table view 覆盖)


2,将这两个控件设置为输出口,分别命名为tableview 和 navgationItem(不能命名为navigationItem,不然会出现如下图(1)报错):


(1)

备注:navigationItem可能是系统内部原先设定好的变量吧,我将控件命名为navgationItem,但我使用下面语句不会报错,而且,如果我继续写self.。。。后面提示中就有navigationItem,如图(2),只是运行成果如图(3),这两句没有在输出口中显示出来,因为我定义的根本不是输出口的性质。

(3)


(3)


3,代码:

(1),.h文件

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>




@property (strong, nonatomic) IBOutlet UITableView *tableview;


@property(strong,nonatomic) NSMutableArray *listTeams;

@property (strong, nonatomic) IBOutlet UINavigationItem *navgationItem;



@end



(2),.m文件:

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    

    self.navigationItem.rightBarButtonItem=self.editButtonItem;

    self.navigationItem.title=@"Move the Table Cell ";

    

    //将当前视图控制器分配给表视图的委托和数据源

    

    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.

}

//协议中必须实现的两种方法


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    

    return [self.listTeams count];

    

}


-(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];

        

    }

    

    NSUInteger row=[indexPath row];

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    cell.textLabel.text=[self.listTeams objectAtIndex:row];

    

    return cell;

    

}



//点击编辑按钮时,就会调用如下方法:(UIViewController生命周期方法,用于响应视图编辑的状态变化)

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{

    

    [super setEditing:editing animated:animated];

    

    [self.tableview setEditing:editing animated:YES];

    

    

}



//单元格图标设置

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    return UITableViewCellEditingStyleNone;

    

}


//是否可以移动单元格

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    

    return YES;

    

}


//重新排序

-(void)tableView:(UITableView *) tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    

    NSString *stringToMove=[self.listTeams objectAtIndex:sourceIndexPath.row];

    [self.listTeams removeObjectAtIndex:sourceIndexPath.row];

    

    [self.listTeams insertObject:stringToMove atIndex:destinationIndexPath.row];

    //在这段代码中,起初我设定错了,选成了[self.listTeams insertObjects:<#(NSArray *)#> atIndexes:<#(NSIndexSet *)#>],尽管我将insertObjects和             atIndex中的s去掉了,写成上面那个样子,但系统还是报错了


}


@end



4,运行结果如图(4),图(5):

                                  
(4) (5)


备注:移动表哥时序图如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值