Cell 的增删和移动

来源:互联网 发布:造价员网络培训 编辑:程序博客网 时间:2024/04/28 04:00


// 设置 Cell的 编辑模式 : 删除 。 增加 ,None

#pragma mark -TableView delegate method

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

   if (indexPath.row==1) {

        returnUITableViewCellEditingStyleInsert;

    }else{

        returnUITableViewCellEditingStyleDelete;

    }

}


/**

 *  2,设置当前视图控制器的"视图"编辑状态

 *  @param editing  是否设置当前控制器视图为可编辑

 *  @param animated 是否有动画效果

 *

 */

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

//    if (self.tableView.editing) {

        //  为 self.tableView视图 调用 setEditing: 方法后,所有的cell都将触发编辑状态

//        [self.tableView setEditing:NO animated:YES];

//    }else{

//        [self.tableView setEditing:YES animated:YES];

//    }

//}



/**

 *   设置 该 indexPath.row 是否可被编辑 ;

 */

// Override to support conditional editing of the table view.

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

{

    // Return NO if you do not want the specified item to be editable.

   if (indexPath.row==0) {

       returnNO;

    }

    return YES;

}




// Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    // delete row

    if (editingStyle ==UITableViewCellEditingStyleDelete) {

        // Delete the row from the data source  :这行注释非常重要,说明了要在这应该做的操作:1,删除数据 2,移除行

        [_fontsArrremoveObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationTop];

    }

    // insert row

    elseif (editingStyle ==UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

        //同样上面的官方注释已经很清楚的说明了,要添加一行cell要作的步骤:1,添加数据 2,插入行

       NSString *newLine=@"New Line";

        [_fontsArrinsertObject:newLineatIndex:indexPath.row+1];

       NSIndexPath *newIndexPath=[NSIndexPathindexPathForRow:indexPath.row+1inSection:indexPath.section];

        [tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationTop];

    }   

}



/**

 *   对Cell 的移动设置

 */


// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

{

  NSString *testFromIndex=[[_fontsArrobjectAtIndex:fromIndexPath.row]retain];

    [_fontsArrremoveObject:testFromIndex];

    [_fontsArrinsertObject:testFromIndexatIndex:toIndexPath.row];

}



/**

 *  tableView cell进行有限制的移动设定;

 *

 */

// 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;

}


对于tableview 的编辑设置方法的分析:

1,setEditing: / setEditing:ainmation:
2, tableView:canMoveRowAtIndexPath:
上面方法1, 调用后回激发 所有cell 的编辑状态
下面方法2, 这个是当我们调用方法一或者左滑动时针对单行或者所有调用的方法 ,当设置为return yes; 时所有的cell都将具备能触发编辑状态,
 方法1,可以不用设置!
0 0
原创粉丝点击