ioS学习 UITableViewCell 增加、删除、移动

@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的内容

        //表中增加行  indexPath1

        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;

//指定单元格的移动

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值