いつもお世話になります。
tableView:moveRowAtIndexPath:toIndexPath:
のなかで取得した、2つのIndexPathによる範囲指定で2次元配列からIndexPathを取得して配列をつくりたいのですが、簡潔な方法はないでしょうか?(説明が下手で申し訳ないです。)
for-inを使用して
enumurate()
やarray.count
から取得したInt
からIndexPath
を生成するしかないでしょうか?
取得した配列[IndexPaht]
はreloadRows(at:with:)
で範囲指定したセルを更新するか、
セルを取得してから特定の値を書き換える目的で使いたいです。
イメージとしては
swift
1let arrayOfArray = [["A", "B", "C", "D"], ["D", "F"], ["G", "H", "I"], ["J", "K"]] 2 3// moveRowAtIndexpath => [0,3] 〜 toIndexPath => [2,1] 4 5// 下記のような配列を取得したい 6let IndexArray: [IndexPath] = [] 7// 中身が、[[0,3],[1,0],[1,1],[2,0],[2,1]] となるようにしたい。 8
一応以下のコードで実現できそうですが、もっとスマートな方法があれば教えてください。
swift
1 2let arrayOfArray = [["A", "B", "C", "D"], ["D", "F"], ["G", "H", "I"], ["J", "K"]] 3 4let startIndex: IndexPath = [0, 3] // moveRowAtIndexpathとtoIndexPathの小さい方を代入する 5let endIndex: IndexPath = [2, 1] // moveRowAtIndexpathとtoIndexPathの大きい方を代入する 6 7var indexArray: [IndexPath] = [] 8 9for (section, array) in arrayOfArray.enumerated() { 10 11 for (row, _) in array.enumerated() { 12 13 if (section < startIndex.section) || 14 (section <= startIndex.section && row < startIndex.row) || 15 (section == endIndex.section && row > endIndex.row) || 16 (section > endIndex.section) { 17 continue 18 } 19 20 let tempIndexPath: IndexPath = [section, row] 21 22 indexArray.append(tempIndexPath) 23 } 24} 25 26print("indexArray:",indexArray) 27 28// => indexArray: [[0, 3], [1, 0], [1, 1], [2, 0], [2, 1]] 29
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/10/10 17:14