@implementation HomeTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
_fontsArray = [NSMutableArray arrayWithArray:[UIFont familyNames]];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//编辑方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
if (self.tableView.editing) {
[self.tableView setEditing:NO animated:YES];
}else{
[self.tableView setEditing:YES animated:YES];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view delegate
//编辑类型,删除,增加,无
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 1) {
return UITableViewCellEditingStyleNone; //0
}else if (indexPath.row == 2){
return UITableViewCellEditingStyleInsert; //2
}else{
return UITableViewCellEditingStyleDelete; //1
}
} //使表视图处于编辑和非编辑状态
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _fontsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.view.bounds.size.width-20, 44)];
label.tag = 101;
label.backgroundColor = [UIColor yellowColor];
[cell.contentView addSubview:label];
}
UILabel *label = (UILabel *)[cell.contentView viewWithTag:101];
label.text = _fontsArray[indexPath.row];
return cell;
}
// 表视图编辑的条件设置
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
if (indexPath.row == 0) {
return NO; // 第一行不编辑
}else{
return YES;}
} //单元格是否能编辑
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source 删除基础数据
[_fontsArray removeObjectAtIndex:indexPath.row];
//删除表中行
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
//增加行
static int rowCount = 1;
NSString *newFont = [NSString stringWithFormat:@"new font %d",rowCount]; //Cell 内容
[_fontsArray insertObject:newFont atIndex:indexPath.row +1]; //数组中往后加一个新cell的内容
//表中增加行 indexPath加1
NSIndexPath *_indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
[tableView insertRowsAtIndexPaths:@[_indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
rowCount++;
}
} //用户编辑时调用
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSLog(@"fromIndexPath %ld",(long)fromIndexPath.row); //原位置
NSLog(@"toIndexPath %ld",(long)toIndexPath.row); //移动后位置
//对应修改数组顺序
NSLog(@"%@",_fontsArray);
NSString *text = [_fontsArray objectAtIndex:fromIndexPath.row];
[_fontsArray removeObject:text];
[_fontsArray insertObject:text atIndex:toIndexPath.row];
NSLog(@"%@",_fontsArray);
} // 移动结束调用
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
} //指定单元格的移动