整的很崩溃的一个程序:
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
备注:移动表哥时序图如下: