回答編集履歴

3

配列のoutputがおかしい

2015/10/10 01:26

投稿

SKYYFISH
SKYYFISH

スコア654

test CHANGED
@@ -42,7 +42,7 @@
42
42
 
43
43
 
44
44
 
45
- // 上記のメソッドを通ると、 contents = {'d','b','c','a','e'} となっているはずです。
45
+ // 上記のメソッドを通ると、 contents = {'b','c','d','a','e'} となっているはずです。
46
46
 
47
47
 
48
48
 

2

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

2015/10/10 01:26

投稿

SKYYFISH
SKYYFISH

スコア654

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

1

言い回しの修正

2015/10/09 11:46

投稿

SKYYFISH
SKYYFISH

スコア654

test CHANGED
@@ -5,6 +5,10 @@
5
5
 
6
6
 
7
7
  以下はtableViewのsetEditing機能を使って実装する時ですが、移動前と移動後のindexPathが取得できますので、、
8
+
9
+
10
+
11
+ 以下のように実装が可能です。
8
12
 
9
13
 
10
14