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

回答編集履歴

5

修正

2016/12/11 01:36

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,4 +1,4 @@
1
- とりあえず思いついた2種類の方法を載せますのでしてみてください。
1
+ とりあえず思いついた2種類の方法を載せますので参考にしてみてください。
2
2
 
3
3
  ★ セルに`IndexPath`のプロパティを作成する
4
4
 

4

修正

2016/12/11 01:36

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,5 +1,4 @@
1
- `IndexPath`が取れればよいのであれば`Cell`側にプロパティで持たせるのが簡単だと思います。
2
- ボタンが押され時に自分`indexPath.row`見れば何番かは分かります。
1
+ とりあえず思いつい2種類方法載せますので試してみてください
3
2
 
4
3
  ★ セルに`IndexPath`のプロパティを作成する
5
4
 

3

修正

2016/12/11 01:35

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  `IndexPath`が取れればよいのであれば`Cell`側にプロパティで持たせるのが簡単だと思います。
2
2
  ボタンが押された時に自分の`indexPath.row`を見れば何番かは分かります。
3
3
 
4
- ★ セルに`indexPath`のプロパティを作成する
4
+ ★ セルに`IndexPath`のプロパティを作成する
5
5
 
6
6
  ```swift
7
7
  // セル作成
@@ -21,7 +21,7 @@
21
21
  }
22
22
  ```
23
23
 
24
- ★ ボタンの`tag`に`indexPath.row`を設定する
24
+ セルボタンの`tag`に`indexPath.row`を設定する
25
25
 
26
26
  ```swift
27
27
  // セル作成

2

修正

2016/12/11 01:30

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,6 +1,8 @@
1
1
  `IndexPath`が取れればよいのであれば`Cell`側にプロパティで持たせるのが簡単だと思います。
2
2
  ボタンが押された時に自分の`indexPath.row`を見れば何番かは分かります。
3
3
 
4
+ ★ セルに`indexPath`のプロパティを作成する
5
+
4
6
  ```swift
5
7
  // セル作成
6
8
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
@@ -17,4 +19,25 @@
17
19
  print(indexPath.row)
18
20
  }
19
21
  }
22
+ ```
23
+
24
+ ★ ボタンの`tag`に`indexPath.row`を設定する
25
+
26
+ ```swift
27
+ // セル作成
28
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
29
+ let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath as IndexPath) as! CustomCell
30
+ cell.button.tag = indexPath.row
31
+ return cell
32
+ }
33
+
34
+ // カスタムセルクラス
35
+ class CustomCell: UITableViewCell {
36
+
37
+ @IBOutlet weak var button: UIButton!
38
+
39
+ @IBAction func pushCellButton(_ sender: UIButton) {
40
+ print(sender.tag)
41
+ }
42
+ }
20
43
  ```

1

修正

2016/12/11 01:16

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -1,5 +1,5 @@
1
1
  `IndexPath`が取れればよいのであれば`Cell`側にプロパティで持たせるのが簡単だと思います。
2
- ボタンが押された時自分の`indexPath.row`を見れば何番かは分かります。
2
+ ボタンが押された時自分の`indexPath.row`を見れば何番かは分かります。
3
3
 
4
4
  ```swift
5
5
  // セル作成