2017-09-12 11 views
0

私はセルが1つのセクションの下にある場合、インターネットはそれでいっぱいですが、2つのセクションを持つものとは苦労しているので、セルをスワップする方法は知っています。明らかにインデックスが範囲外であるため、すべてのセルを移動することはできません。 viewDidLoadでセクション間でセルを並べ替える

let sections: [String] = ["Box", "Inventory"] 
    var s1Data: [UIImage] = [] // 
    var s2Data: [UIImage] = [] //these are filled by other function 

let sectionsImages: [UIImage] = [#imageLiteral(resourceName: "blackBox"), #imageLiteral(resourceName: "blackBag")] 

var sectionData: [[UIImage]] = [] 

を():

tableView.isEditing = true 


tableView.delegate = self 
tableView.dataSource = self 

sectionData = [s1Data, s2Data] 

その後はかなり私が宣言しているので、私は唯一の重要なビット

を貼り付けます、全体のコードをポストする必要だろうと思いましたtableView関数の数が、私が通過することができない1および私が話している1:

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

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

Aしたがって、「境界外」のエラーが発生したため、最後のイメージセルを交換しようとするまで、スワッピングはうまく行きます。私はアイテムを次のように宣言しなければならないことを知っています:

let item = sectionData[sourceIndexPath.section][sourceIndexPath.row] 

「削除」と「挿入」はどうですか?私はあなたの助け

EDITに感謝だろう :

それは簡単な方法の一つである場合、私は知らないが、私は、それをやりました。とにかく:

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) 
{ 
    if sourceIndexPath.section == 0 && destinationIndexPath.section == 0 
    { 
     let item = sectionData[0][sourceIndexPath.row] 
     sectionData[0].remove(at: sourceIndexPath.row) 
     sectionData[0].insert(item, at: destinationIndexPath.row) 
    } 

    else if sourceIndexPath.section == 0 && destinationIndexPath.section == 1 
    { 
     let item = sectionData[0][sourceIndexPath.row] 
     sectionData[0].remove(at: sourceIndexPath.row) 
     sectionData[1].insert(item, at: destinationIndexPath.row) 
    } 

    else if sourceIndexPath.section == 1 && destinationIndexPath.section == 0 
    { 
     let item = sectionData[1][sourceIndexPath.row] 
     sectionData[1].remove(at: sourceIndexPath.row) 
     sectionData[0].insert(item, at: destinationIndexPath.row) 
    } 

    else if sourceIndexPath.section == 1 && destinationIndexPath.section == 1 
    { 
     let item = sectionData[1][sourceIndexPath.row] 
     sectionData[1].remove(at: sourceIndexPath.row) 
     sectionData[1].insert(item, at: destinationIndexPath.row) 
    } 

    else 
    { 
     print("ERROR - SWAP MALFUNCTION") 
    } 
} 

答えて

0

これは私がそれをやった方法です、インサートはdestinationIndexPath.sectionを使用しています。

groupItems[sourceIndexPath.section].remove(at: sourceIndexPath.row) 
groupItems[destinationIndexPath.section].insert(movedObject, at: destinationIndexPath.row) 
関連する問題