回答編集履歴

2

listtableを使用するように変更。

2015/12/10 05:40

投稿

fuzzball
fuzzball

スコア16731

test CHANGED
@@ -46,10 +46,10 @@
46
46
 
47
47
  NSMutableArray *val;
48
48
 
49
-
50
-
51
49
  int iUserCnt;
52
50
 
51
+ UITableView *listtable;
52
+
53
53
  }
54
54
 
55
55
  @property (retain, nonatomic)NSMutableArray *hoge;
@@ -76,15 +76,15 @@
76
76
 
77
77
  //テーブル生成
78
78
 
79
- UITableView *table = [[UITableView alloc] initWithFrame:self.view.frame];
79
+ listtable = [[UITableView alloc] initWithFrame:self.view.frame];
80
-
80
+
81
- [self.view addSubview:table];
81
+ [self.view addSubview:listtable];
82
-
82
+
83
- table.dataSource = self;
83
+ listtable.dataSource = self;
84
-
84
+
85
- table.delegate = self;
85
+ listtable.delegate = self;
86
-
86
+
87
- [table registerClass:UITableViewCell.class forCellReuseIdentifier:_REUSEID];
87
+ [listtable registerClass:UITableViewCell.class forCellReuseIdentifier:_REUSEID];
88
88
 
89
89
 
90
90
 
@@ -188,15 +188,13 @@
188
188
 
189
189
  [self DisplayList];
190
190
 
191
- [tableView reloadData];
192
-
193
191
  }
194
192
 
195
193
 
196
194
 
197
195
  //hoge更新
198
196
 
199
- // reloadDataを外に出した以外は変更無しです
197
+ // reloadDataをループの外に出した以外は変更無しです
200
198
 
201
199
  -(void)DisplayList
202
200
 
@@ -230,6 +228,8 @@
230
228
 
231
229
  }
232
230
 
231
+ [listtable reloadData];
232
+
233
233
  }
234
234
 
235
235
 

1

サンプルコード追加。

2015/12/10 05:40

投稿

fuzzball
fuzzball

スコア16731

test CHANGED
@@ -11,3 +11,243 @@
11
11
  ```
12
12
 
13
13
  とすればテーブルが更新されるはずです。
14
+
15
+
16
+
17
+ ---
18
+
19
+
20
+
21
+ 【追記】
22
+
23
+
24
+
25
+ サンプルコードです。
26
+
27
+ セルをタップするとセルが増えていきます。
28
+
29
+
30
+
31
+ ```objectivec
32
+
33
+ #import "ViewController.h"
34
+
35
+
36
+
37
+ @interface ViewController ()
38
+
39
+ {
40
+
41
+ NSMutableDictionary *m_UserListDict;
42
+
43
+ NSString *UserCnt;
44
+
45
+ NSString *str;
46
+
47
+ NSMutableArray *val;
48
+
49
+
50
+
51
+ int iUserCnt;
52
+
53
+ }
54
+
55
+ @property (retain, nonatomic)NSMutableArray *hoge;
56
+
57
+ @end
58
+
59
+
60
+
61
+ #define _REUSEID @"hoge"
62
+
63
+
64
+
65
+ @implementation ViewController
66
+
67
+
68
+
69
+ - (void)viewDidLoad
70
+
71
+ {
72
+
73
+ [super viewDidLoad];
74
+
75
+
76
+
77
+ //テーブル生成
78
+
79
+ UITableView *table = [[UITableView alloc] initWithFrame:self.view.frame];
80
+
81
+ [self.view addSubview:table];
82
+
83
+ table.dataSource = self;
84
+
85
+ table.delegate = self;
86
+
87
+ [table registerClass:UITableViewCell.class forCellReuseIdentifier:_REUSEID];
88
+
89
+
90
+
91
+ //m_UserListDict初期化
92
+
93
+ m_UserListDict =
94
+
95
+ @{
96
+
97
+ @"UserCnt": @"3",
98
+
99
+ @"0": @{@"name": @"zero"},
100
+
101
+ @"1": @{@"name": @"one"},
102
+
103
+ @"2": @{@"name": @"two"},
104
+
105
+ }.mutableCopy;
106
+
107
+ //NSLog(@"%@", m_UserListDict);
108
+
109
+
110
+
111
+ //hoge初期化
112
+
113
+ [self DisplayList];
114
+
115
+ }
116
+
117
+
118
+
119
+ //セル数を返す
120
+
121
+ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
122
+
123
+ {
124
+
125
+ return self.hoge.count;
126
+
127
+ }
128
+
129
+
130
+
131
+ //セル生成
132
+
133
+ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
134
+
135
+ {
136
+
137
+ //セル取得
138
+
139
+ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_REUSEID];
140
+
141
+ //セル設定
142
+
143
+ cell.textLabel.text = self.hoge[indexPath.row];
144
+
145
+
146
+
147
+ return cell;
148
+
149
+ }
150
+
151
+
152
+
153
+ //セルタップ時に呼ばれる
154
+
155
+ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
156
+
157
+ {
158
+
159
+ //m_UserListDictに追加 (この中はあまり気にしないで下さい)
160
+
161
+ {
162
+
163
+ //key生成
164
+
165
+ NSString *key = @(self.hoge.count).stringValue;
166
+
167
+ //obj生成
168
+
169
+ NSNumberFormatter *nf = NSNumberFormatter.new;
170
+
171
+ nf.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
172
+
173
+ nf.numberStyle = NSNumberFormatterSpellOutStyle;
174
+
175
+ NSString *name = [nf stringFromNumber:@(self.hoge.count)];
176
+
177
+ m_UserListDict[key] = @{@"name": name};
178
+
179
+ //UserCnt+1
180
+
181
+ m_UserListDict[@"UserCnt"] = @([m_UserListDict[@"UserCnt"] integerValue] + 1).stringValue;
182
+
183
+ }
184
+
185
+
186
+
187
+ //更新
188
+
189
+ [self DisplayList];
190
+
191
+ [tableView reloadData];
192
+
193
+ }
194
+
195
+
196
+
197
+ //hoge更新
198
+
199
+ // reloadDataを外に出した以外は変更無しです
200
+
201
+ -(void)DisplayList
202
+
203
+ {
204
+
205
+ str = [[NSString alloc]init];
206
+
207
+ val = [[NSMutableArray alloc]init];
208
+
209
+ self.hoge = [[NSMutableArray alloc]init];
210
+
211
+
212
+
213
+ UserCnt = [m_UserListDict objectForKey:@"UserCnt"];
214
+
215
+ iUserCnt = [UserCnt intValue];
216
+
217
+ NSLog(@"UserCnt=%zd", iUserCnt);
218
+
219
+
220
+
221
+ for ( int i = 0; i < iUserCnt; i++){
222
+
223
+
224
+
225
+ str = [NSString stringWithFormat:@"%d", i];
226
+
227
+ val = [[m_UserListDict objectForKey:str] objectForKey:@"name"];
228
+
229
+ [self.hoge addObject:val];
230
+
231
+ }
232
+
233
+ }
234
+
235
+
236
+
237
+ - (void)didReceiveMemoryWarning
238
+
239
+ {
240
+
241
+ [super didReceiveMemoryWarning];
242
+
243
+ }
244
+
245
+
246
+
247
+ @end
248
+
249
+
250
+
251
+ ```
252
+
253
+