回答編集履歴

3

selectionStyle についてのコードを追加

2017/09/22 01:17

投稿

ykws
ykws

スコア1236

test CHANGED
@@ -14,6 +14,20 @@
14
14
 
15
15
  ```swift
16
16
 
17
+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
18
+
19
+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
20
+
21
+ cell.textLabel?.text = Haru[indexPath.row]
22
+
23
+ cell.selectionStyle = (Haru.count > (indexPath.row + 1)) ? .none : .blue
24
+
25
+ return cell
26
+
27
+ }
28
+
29
+
30
+
17
31
  override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
18
32
 
19
33
  if Haru.count > (indexPath.row + 1) {
@@ -27,3 +41,11 @@
27
41
  }
28
42
 
29
43
  ```
44
+
45
+
46
+
47
+ ### 参考
48
+
49
+
50
+
51
+ [UITableViewのセルを選択不可にする方法](https://qiita.com/takashings/items/36b820f09fb19edd7556)

2

willSelectRowAt メソッド中への記載を明記

2017/09/22 01:17

投稿

ykws
ykws

スコア1236

test CHANGED
@@ -14,14 +14,16 @@
14
14
 
15
15
  ```swift
16
16
 
17
- if Haru.count > (indexPath.row + 1) {
17
+ override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
18
18
 
19
+ if Haru.count > (indexPath.row + 1) {
20
+
19
- return nil
21
+ return nil
22
+
23
+ }
24
+
25
+ return indexPath
20
26
 
21
27
  }
22
28
 
23
-
24
-
25
- return indexPath
26
-
27
29
  ```

1

一番下のセルであることに注目した判定方法を追記

2017/09/21 06:14

投稿

ykws
ykws

スコア1236

test CHANGED
@@ -3,3 +3,25 @@
3
3
 
4
4
 
5
5
  [IndexPath](https://developer.apple.com/documentation/foundation/indexpath) には `section` と `row` があって、今回 `section` は一つしかないので、`indexPath.row` がセルの位置と等しくなっています。
6
+
7
+
8
+
9
+ 今回のコードだと、 **一番下のセル** が反応してほしくないので次のように判定する方法があります。
10
+
11
+ 他にも考え方はあるので参考の一つとしてください。
12
+
13
+
14
+
15
+ ```swift
16
+
17
+ if Haru.count > (indexPath.row + 1) {
18
+
19
+ return nil
20
+
21
+ }
22
+
23
+
24
+
25
+ return indexPath
26
+
27
+ ```