2017-07-27 168 views
1

我試圖根據它們的優先級(枚舉)重新排序單元格。我有雙重檢查,我的枚舉值是正確的。這是我的枚舉重新排序表格單元格

enum Priority{ 
    case low 
    case neutral 
    case high 
    case urgent 
} 

正如你所看到的,較低的優先級具有較低的散列值。我試圖比較兩個單元格;一個細胞和它上面的細胞。如果單元格的值大於它上面的值,它會向上移動。我獲得小區的IndexPath如上所述使用

var newIndexPath = IndexPath(row: indexPath.row - 1 , section: indexPath.section) 

比較使用

if(comparePriority.hashValue < priorityValue.hashValue) 

我嘗試切換使用

tableview.moveRow(at: indexPath, to: newIndexPath) 

結果是有趣的順序枚舉值後,我將使用圖片來解釋。請注意,所有這些代碼都在我的cellForRowAt函數中,除了「test」之外,所有單元格的優先級都與它們的名稱相對應,並且這些圖片是以慢動作拍攝的,這意味着用戶將無法真正實現看到開關發生。

中秋節SEGUE Cells start switching

細胞完成切換 Cells finish switching

+0

所以你要設置動畫的運動?否則,在對數組進行排序後reloadData將會爲你工作。 – adev

回答

2

只是有點你根據這些重點項目的數組,然後做一個表重裝。

通過這種方式,索引0只是第一個項目等,您將獲得簡單的代碼,不會因爲即時排序/比較而變得混亂。

排序看起來是這樣的(你做出enumInt型之後,並更名爲priorityValuepriority我建議你做):

enum Priority: Int 
{ 
    case low  = 0 
    case neutral = 1 
    case high = 2 
    case urgent = 3 
} 

items.sorted(by: { $0.priority < $1.priority }) 
+0

我會如何按照正確的順序排列我的數組? – williej926

0

您更好地設定優先級值明確這樣。

enum Priority: Int { 
case low = 0 
case neutral = 1 
case high = 2 
case urgent = 3 
} 

,並按照在dataSource陣列狀

dataSource = dataSource.sorted(by: { $0.priority < $1.priority }) 
tableView.reloadDate()