teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

配列のoutputがおかしい

2015/10/10 01:26

投稿

SKYYFISH
SKYYFISH

スコア654

answer CHANGED
@@ -20,7 +20,7 @@
20
20
  tableViewObject.reloadData() //テーブル更新
21
21
  }
22
22
 
23
- // 上記のメソッドを通ると、 contents = {'d','b','c','a','e'} となっているはずです。
23
+ // 上記のメソッドを通ると、 contents = {'b','c','d','a','e'} となっているはずです。
24
24
 
25
25
  ```
26
26
 

2

iPhoneから投稿すると回答が雑になりますね。

2015/10/10 01:26

投稿

SKYYFISH
SKYYFISH

スコア654

answer CHANGED
@@ -1,24 +1,28 @@
1
- 同じようなコードを書きました。
1
+ 同じようなコードを書ことがあります
2
- 結論から言うと、動的に並び替えを行うメソッドで配列の順番を並び替えて実装ます。
2
+ 結論から言うと、動的に並び替えを行うメソッドで配列の順番を並び替えて実装できます。
3
3
 
4
- 以下はtableViewのsetEditing機能を使って実装する時ですが、移動前と移動後のindexPathが取得できますので、、
4
+ 以下はtableViewのsetEditing機能を使って実装する時ですが、
5
+ 移動前と移動後のindexPathが取得できますので、以下のように実装が可能です。
5
6
 
6
- 以下のように実装が可能です。
7
+ ```swift
7
8
 
9
+ //contents:String[] = {'a','b','c','d','e'}
8
- ```swift
10
+ //sourceIndexPath = 0
11
+ //destinationIndexPath = 3 だったとして、、
12
+
9
13
  func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
10
-
11
- let src:Int = sourceIndexPath.row
14
+ let src:Int = sourceIndexPath.row //簡単のため、変数名をわかりやすくしておきますね。
12
15
  let dst:Int = destinationIndexPath.row
16
+
13
- let cacheF:String = contents[sourceIndexPath.row] as! String
17
+ let cacheF:String = contents[src] as! String // srcの位置にある値をキャッシュ
14
- let cacheF2:String = checkMark[sourceIndexPath.row] as! String
15
- contents.removeObjectAtIndex(src)
18
+ contents.removeObjectAtIndex(src) //srcの位置にある値を削除
16
- contents.insertObject(cacheF, atIndex: dst)
19
+ contents.insertObject(cacheF, atIndex: dst) //dstの位置にキャッシュした値を格納
17
- checkMark.removeObjectAtIndex(src)
18
- checkMark.insertObject(cacheF2, atIndex: dst)
19
- tableViewObject.reloadData() //これが無いとテーブル更新されない;;
20
+ tableViewObject.reloadData() //テーブル更新
20
-
21
21
  }
22
+
23
+ // 上記のメソッドを通ると、 contents = {'d','b','c','a','e'} となっているはずです。
24
+
22
25
  ```
23
26
 
27
+ 私の場合は配列の中身がStringでしたが、
24
- 私の場合は配列の中身がStringでしたが、これをViewControllerのインスタンスにすれば要件の動作ができると思います。
28
+ これをViewControllerのインスタンスにすれば要件の動作ができると思います。

1

言い回しの修正

2015/10/09 11:46

投稿

SKYYFISH
SKYYFISH

スコア654

answer CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  以下はtableViewのsetEditing機能を使って実装する時ですが、移動前と移動後のindexPathが取得できますので、、
5
5
 
6
+ 以下のように実装が可能です。
7
+
6
8
  ```swift
7
9
  func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
8
10