2016-10-05 2 views
0

私は編集可能なテーブルビューを持っています。すべての行を移動したり、一部を削除したりすることができます。編集スタイルが異なるUITableView、削除後のグリッチ

問題:行番号1を削除してから、行番号2の並べ替えボタンをタッチすると、インデントが失われます。 何か間違っているのですか?これはバグですか?

Screenshot link テストプロジェクト:https://drive.google.com/open?id=0Bycu0Xewdx_XZlhwcW9ZQlFEcnc

class TableViewController: UITableViewController { 

var items = ["0", "1", "2", "3", "4"] 


override func viewDidLoad() { 

    self.navigationItem.setRightBarButton(self.editButtonItem, animated: false) 
} 



override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return self.items.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 

    cell.textLabel?.text = self.items[indexPath.row] 

    // set background colors for visibility 
    cell.textLabel?.backgroundColor = UIColor.green 
    cell.contentView.backgroundColor = UIColor.yellow 

    return cell 
} 



override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 

    return true 
} 

// MARK: - Delete 
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 

    if self.items[indexPath.row] == "1" || self.items[indexPath.row] == "3" { 

     return UITableViewCellEditingStyle.delete 
    } 


    return UITableViewCellEditingStyle.none 
} 

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 

    if editingStyle == .delete { 

     self.items.remove(at: indexPath.row) 
     tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
    } 
} 


// MARK: - Move 
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 

    return true 
} 

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 

    let item = self.items[sourceIndexPath.row] 
    self.items.remove(at: sourceIndexPath.row) 
    self.items.insert(item, at: destinationIndexPath.row) 
} 

}

答えて

0

自己答えは、deleteRowをした後、次のセルがfalseに設定さshouldIndentWhileEditingされることを表示されます。

だから、解決策は、それを元に戻すことです。

 if let cell = tableView.cellForRow(at: indexPath), cell.shouldIndentWhileEditing == false { 
      cell.shouldIndentWhileEditing = true 
     } 
関連する問題