質問編集履歴

3

クラス内容を少し修正

2016/04/07 03:08

投稿

cicle
cicle

スコア35

test CHANGED
File without changes
test CHANGED
@@ -74,178 +74,192 @@
74
74
 
75
75
  class CellManager: NSObject {
76
76
 
77
+
78
+
79
+ enum CellType {
80
+
81
+ case Cell_1
82
+
83
+ case Cell_2
84
+
85
+ }
86
+
87
+
88
+
89
+ var createCellTypes: [CellType] = [];
90
+
77
91
 
78
92
 
79
- static func createCell(tableView: UITableView, indexPath: NSIndexPath) -> BaseTableCell? {
93
+ func createCell(tableView: UITableView, indexPath: NSIndexPath) -> BaseTableCell? {
80
94
 
81
95
 
82
96
 
83
- if (indexPath.row == 0) {
97
+ switch createCellTypes[indexPath.row] {
98
+
99
+ case .Cell_1:
84
100
 
85
101
  return Cell_1.createCellForIdentifer(tableView, indexPath: indexPath)
86
102
 
103
+ default:
104
+
105
+ return Cell_2.createCellForIdentifer(tableView, indexPath: indexPath)
106
+
87
107
  }
88
108
 
109
+ }
110
+
111
+ }
112
+
113
+
114
+
115
+ ```
116
+
117
+ という、Cellの生成等を司るクラス**CellManager**を用意しました。
118
+
119
+
120
+
121
+ 次に、カスタムCellのベースになるクラス**BaseTableCell**を用意しました。
122
+
123
+
124
+
125
+ ```swift
126
+
127
+
128
+
129
+ class BaseTableCell: UITableViewCell {
130
+
131
+
132
+
89
- else if (indexPath.row%2 == 0) {
133
+ var cellIdentifier:String {
134
+
90
-
135
+ get {
136
+
91
- return Cell_2.createCellForIdentifer(tableView, indexPath: indexPath)
137
+ return ""
92
138
 
93
139
  }
94
140
 
95
- else {
141
+ }
96
-
142
+
143
+
144
+
97
- return Cell_1.createCellForIdentifer(tableView, indexPath: indexPath)
145
+ class func createCellForIdentifer(tableView: UITableView, indexPath:NSIndexPath) -> BaseTableCell?
146
+
147
+ {
148
+
149
+ return nil;
150
+
151
+ }
152
+
153
+
154
+
155
+ func setContentData(data:AnyObject){}
156
+
157
+
158
+
159
+ override func awakeFromNib() {
160
+
161
+ super.awakeFromNib()
162
+
163
+ // Initialization code
164
+
165
+ }
166
+
167
+ override func setSelected(selected: Bool, animated: Bool) {
168
+
169
+ super.setSelected(selected, animated: animated)
170
+
171
+
172
+
173
+ // Configure the view for the selected state
174
+
175
+ }
176
+
177
+ }
178
+
179
+ ```
180
+
181
+
182
+
183
+ BaseTableCellのサブクラスとして__**Cell_1**__を作ります。
184
+
185
+ ```swift
186
+
187
+ import UIKit
188
+
189
+
190
+
191
+ class Cell_1: BaseTableCell {
192
+
193
+
194
+
195
+ override var cellIdentifier: String {
196
+
197
+ get {
198
+
199
+ return "cell_1"
98
200
 
99
201
  }
100
202
 
101
203
  }
102
204
 
205
+ override class func createCellForIdentifer(tableView: UITableView, indexPath: NSIndexPath) -> BaseTableCell? {
206
+
207
+ let cell = tableView.dequeueReusableCellWithIdentifier("cell_1", forIndexPath: indexPath) as! Cell_1
208
+
209
+ return cell;
210
+
211
+ }
212
+
213
+
214
+
215
+ override func setContentData(data: AnyObject) {
216
+
217
+ print(self.cellIdentifier);
218
+
219
+ }
220
+
103
221
  }
104
222
 
105
223
 
106
224
 
107
225
  ```
108
226
 
109
- という、Cellの生成等を司るクラス**CellManager**を用意しました。
227
+ こんな感じで**Cell_2**クラスも作りました。
110
-
111
-
112
-
228
+
229
+
230
+
113
- 次に、カスタムCellのベースになるクラス**BaseTableCell**を用意しました。
231
+ ViewControllerで
114
-
115
-
116
-
232
+
117
- ```swift
233
+ ```swift
118
-
119
-
120
-
234
+
235
+
236
+
237
+
238
+
121
- class BaseTableCell: UITableViewCell {
239
+ let cellManager = CellManager();
122
-
123
-
124
-
240
+
241
+
242
+
125
- var cellIdentifier:String {
243
+ override func viewDidLoad() {
126
-
127
- get {
244
+
128
-
129
- return ""
245
+ super.viewDidLoad()
246
+
130
-
247
+ // Do any additional setup after loading the view, typically from a nib.
248
+
249
+ tableView.delegate = self
250
+
251
+ tableView.dataSource = self
252
+
253
+
254
+
255
+ cellManager.createCellTypes = [.Cell_1, .Cell_2, .Cell_2, .Cell_2, .Cell_1]
256
+
131
- }
257
+ }
132
-
133
- }
258
+
259
+
134
260
 
135
261
 
136
262
 
137
- class func createCellForIdentifer(tableView: UITableView, indexPath:NSIndexPath) -> BaseTableCell?
138
-
139
- {
140
-
141
- return nil;
142
-
143
- }
144
-
145
-
146
-
147
- func setContentData(data:AnyObject){}
148
-
149
-
150
-
151
- override func awakeFromNib() {
152
-
153
- super.awakeFromNib()
154
-
155
- // Initialization code
156
-
157
- }
158
-
159
- override func setSelected(selected: Bool, animated: Bool) {
160
-
161
- super.setSelected(selected, animated: animated)
162
-
163
-
164
-
165
- // Configure the view for the selected state
166
-
167
- }
168
-
169
- }
170
-
171
- ```
172
-
173
-
174
-
175
- BaseTableCellのサブクラスとして__**Cell_1**__を作ります。
176
-
177
- ```swift
178
-
179
- import UIKit
180
-
181
-
182
-
183
- class Cell_1: BaseTableCell {
184
-
185
-
186
-
187
- override var cellIdentifier: String {
188
-
189
- get {
190
-
191
- return "cell_1"
192
-
193
- }
194
-
195
- }
196
-
197
- override class func createCellForIdentifer(tableView: UITableView, indexPath: NSIndexPath) -> BaseTableCell? {
198
-
199
- let cell = tableView.dequeueReusableCellWithIdentifier("cell_1", forIndexPath: indexPath) as! Cell_1
200
-
201
- return cell;
202
-
203
- }
204
-
205
-
206
-
207
- override func setContentData(data: AnyObject) {
208
-
209
- print(self.cellIdentifier);
210
-
211
- }
212
-
213
- }
214
-
215
-
216
-
217
- ```
218
-
219
- こんな感じで**Cell_2**クラスも作りました。
220
-
221
-
222
-
223
- ViewControllerで
224
-
225
- ```swift
226
-
227
- override func viewDidLoad() {
228
-
229
- super.viewDidLoad()
230
-
231
- // Do any additional setup after loading the view, typically from a nib.
232
-
233
- tableView.delegate = self
234
-
235
- tableView.dataSource = self
236
-
237
-
238
-
239
- tableView.registerClass(Cell_1.self, forCellReuseIdentifier: "cell_1")
240
-
241
- tableView.registerClass(Cell_2.self, forCellReuseIdentifier: "cell_2")
242
-
243
- }
244
-
245
-
246
-
247
-
248
-
249
263
  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
250
264
 
251
265
  return 30

2

内容追加

2016/04/07 03:08

投稿

cicle
cicle

スコア35

test CHANGED
File without changes
test CHANGED
@@ -57,3 +57,221 @@
57
57
 
58
58
 
59
59
  よろしくお願いします。
60
+
61
+
62
+
63
+ //////////////////
64
+
65
+ 4月7日 追記
66
+
67
+
68
+
69
+ こういうのを考えたんですがどうでしょうか?
70
+
71
+ テストで試したのでTableViewですが...
72
+
73
+ ```swift
74
+
75
+ class CellManager: NSObject {
76
+
77
+
78
+
79
+ static func createCell(tableView: UITableView, indexPath: NSIndexPath) -> BaseTableCell? {
80
+
81
+
82
+
83
+ if (indexPath.row == 0) {
84
+
85
+ return Cell_1.createCellForIdentifer(tableView, indexPath: indexPath)
86
+
87
+ }
88
+
89
+ else if (indexPath.row%2 == 0) {
90
+
91
+ return Cell_2.createCellForIdentifer(tableView, indexPath: indexPath)
92
+
93
+ }
94
+
95
+ else {
96
+
97
+ return Cell_1.createCellForIdentifer(tableView, indexPath: indexPath)
98
+
99
+ }
100
+
101
+ }
102
+
103
+ }
104
+
105
+
106
+
107
+ ```
108
+
109
+ という、Cellの生成等を司るクラス**CellManager**を用意しました。
110
+
111
+
112
+
113
+ 次に、カスタムCellのベースになるクラス**BaseTableCell**を用意しました。
114
+
115
+
116
+
117
+ ```swift
118
+
119
+
120
+
121
+ class BaseTableCell: UITableViewCell {
122
+
123
+
124
+
125
+ var cellIdentifier:String {
126
+
127
+ get {
128
+
129
+ return ""
130
+
131
+ }
132
+
133
+ }
134
+
135
+
136
+
137
+ class func createCellForIdentifer(tableView: UITableView, indexPath:NSIndexPath) -> BaseTableCell?
138
+
139
+ {
140
+
141
+ return nil;
142
+
143
+ }
144
+
145
+
146
+
147
+ func setContentData(data:AnyObject){}
148
+
149
+
150
+
151
+ override func awakeFromNib() {
152
+
153
+ super.awakeFromNib()
154
+
155
+ // Initialization code
156
+
157
+ }
158
+
159
+ override func setSelected(selected: Bool, animated: Bool) {
160
+
161
+ super.setSelected(selected, animated: animated)
162
+
163
+
164
+
165
+ // Configure the view for the selected state
166
+
167
+ }
168
+
169
+ }
170
+
171
+ ```
172
+
173
+
174
+
175
+ BaseTableCellのサブクラスとして__**Cell_1**__を作ります。
176
+
177
+ ```swift
178
+
179
+ import UIKit
180
+
181
+
182
+
183
+ class Cell_1: BaseTableCell {
184
+
185
+
186
+
187
+ override var cellIdentifier: String {
188
+
189
+ get {
190
+
191
+ return "cell_1"
192
+
193
+ }
194
+
195
+ }
196
+
197
+ override class func createCellForIdentifer(tableView: UITableView, indexPath: NSIndexPath) -> BaseTableCell? {
198
+
199
+ let cell = tableView.dequeueReusableCellWithIdentifier("cell_1", forIndexPath: indexPath) as! Cell_1
200
+
201
+ return cell;
202
+
203
+ }
204
+
205
+
206
+
207
+ override func setContentData(data: AnyObject) {
208
+
209
+ print(self.cellIdentifier);
210
+
211
+ }
212
+
213
+ }
214
+
215
+
216
+
217
+ ```
218
+
219
+ こんな感じで**Cell_2**クラスも作りました。
220
+
221
+
222
+
223
+ ViewControllerで
224
+
225
+ ```swift
226
+
227
+ override func viewDidLoad() {
228
+
229
+ super.viewDidLoad()
230
+
231
+ // Do any additional setup after loading the view, typically from a nib.
232
+
233
+ tableView.delegate = self
234
+
235
+ tableView.dataSource = self
236
+
237
+
238
+
239
+ tableView.registerClass(Cell_1.self, forCellReuseIdentifier: "cell_1")
240
+
241
+ tableView.registerClass(Cell_2.self, forCellReuseIdentifier: "cell_2")
242
+
243
+ }
244
+
245
+
246
+
247
+
248
+
249
+ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
250
+
251
+ return 30
252
+
253
+ }
254
+
255
+
256
+
257
+ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
258
+
259
+ let cell = CellManager.createCell(tableView, indexPath: indexPath)
260
+
261
+ cell?.setContentData("test");
262
+
263
+ return cell!;
264
+
265
+ }
266
+
267
+ ```
268
+
269
+ こうすると、Cellの表示・非表示などが**CellManager**をいじるだけでスッキリ簡単に出来るのかな?と思っております。
270
+
271
+
272
+
273
+ もし、ご意見がありましたらコメントして頂けますとありがたいです。
274
+
275
+
276
+
277
+ よろしくお願いします。

1

タイトルの誤記入の修正

2016/04/07 02:57

投稿

cicle
cicle

スコア35

test CHANGED
@@ -1 +1 @@
1
- UITableCollectionViewのCellの変動対応
1
+ UICollectionViewのCellの変動対応
test CHANGED
File without changes