iOS 開發之如何編輯tableView上的cell

效果圖展示:







plist 文件讀取和調用

    path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"DetailData.plist"];

 

    list = [[NSArray arrayWithContentsOfFile:path]mutableCopy];

  

1、添加導航控制器,在導航控制器上添加兩個 navigationItem 編輯 和 添加

2、點擊編輯的時候調用方法

1⃣️//點擊一次開始編輯,點擊兩次結束編輯

- (void)edit

{

    [myTableView setEditing:!myTableView.isEditing animated:YES];

}

2⃣️編輯tableView 上的cell

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

{

    

//    判斷編輯中的樣式 是否是 刪除的樣式

    if (editingStyle == UITableViewCellEditingStyleDelete ) {

        

//        1、刪除數據

//        2、更新視圖


//        移除數組裏面的數據

        [list removeObjectAtIndex:indexPath.row];

//        把移除後的數據 同步到 plist 裏面

      BOOL success = [list writeToFile:path atomically:YES];

//        如果數據同步成功 ,就刪除數據裏面的cell

        if (success) {

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

            

        }    

    }

}

3⃣️、cell之間的移動

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

{

//    如果移動的不是同一個位置 才進行操作

    if (sourceIndexPath.row != destinationIndexPath.row) {

        

//       1 保存 需要移動的數據

        NSDictionary *info = list[sourceIndexPath.row];

//       2、移除 需要移動的數據

        [list removeObjectAtIndex:sourceIndexPath.row];

//       3、插入數據

//        destinationIndexPath 可以得到 咱們要移動的位置

        [list insertObject:info atIndex:destinationIndexPath.row];

//       4、同步數據到plist

        [list writeToFile:path atomically:YES];   

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章