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

回答編集履歴

2

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

2015/12/10 05:40

投稿

fuzzball
fuzzball

スコア16733

answer CHANGED
@@ -22,8 +22,8 @@
22
22
  NSString *UserCnt;
23
23
  NSString *str;
24
24
  NSMutableArray *val;
25
-
26
25
  int iUserCnt;
26
+ UITableView *listtable;
27
27
  }
28
28
  @property (retain, nonatomic)NSMutableArray *hoge;
29
29
  @end
@@ -37,11 +37,11 @@
37
37
  [super viewDidLoad];
38
38
 
39
39
  //テーブル生成
40
- UITableView *table = [[UITableView alloc] initWithFrame:self.view.frame];
40
+ listtable = [[UITableView alloc] initWithFrame:self.view.frame];
41
- [self.view addSubview:table];
41
+ [self.view addSubview:listtable];
42
- table.dataSource = self;
42
+ listtable.dataSource = self;
43
- table.delegate = self;
43
+ listtable.delegate = self;
44
- [table registerClass:UITableViewCell.class forCellReuseIdentifier:_REUSEID];
44
+ [listtable registerClass:UITableViewCell.class forCellReuseIdentifier:_REUSEID];
45
45
 
46
46
  //m_UserListDict初期化
47
47
  m_UserListDict =
@@ -93,11 +93,10 @@
93
93
 
94
94
  //更新
95
95
  [self DisplayList];
96
- [tableView reloadData];
97
96
  }
98
97
 
99
98
  //hoge更新
100
- // reloadDataを外に出した以外は変更無しです
99
+ // reloadDataをループの外に出した以外は変更無しです
101
100
  -(void)DisplayList
102
101
  {
103
102
  str = [[NSString alloc]init];
@@ -114,6 +113,7 @@
114
113
  val = [[m_UserListDict objectForKey:str] objectForKey:@"name"];
115
114
  [self.hoge addObject:val];
116
115
  }
116
+ [listtable reloadData];
117
117
  }
118
118
 
119
119
  - (void)didReceiveMemoryWarning

1

サンプルコード追加。

2015/12/10 05:40

投稿

fuzzball
fuzzball

スコア16733

answer CHANGED
@@ -4,4 +4,123 @@
4
4
  ```objectivec
5
5
  [tableView reloadData];
6
6
  ```
7
- とすればテーブルが更新されるはずです。
7
+ とすればテーブルが更新されるはずです。
8
+
9
+ ---
10
+
11
+ 【追記】
12
+
13
+ サンプルコードです。
14
+ セルをタップするとセルが増えていきます。
15
+
16
+ ```objectivec
17
+ #import "ViewController.h"
18
+
19
+ @interface ViewController ()
20
+ {
21
+ NSMutableDictionary *m_UserListDict;
22
+ NSString *UserCnt;
23
+ NSString *str;
24
+ NSMutableArray *val;
25
+
26
+ int iUserCnt;
27
+ }
28
+ @property (retain, nonatomic)NSMutableArray *hoge;
29
+ @end
30
+
31
+ #define _REUSEID @"hoge"
32
+
33
+ @implementation ViewController
34
+
35
+ - (void)viewDidLoad
36
+ {
37
+ [super viewDidLoad];
38
+
39
+ //テーブル生成
40
+ UITableView *table = [[UITableView alloc] initWithFrame:self.view.frame];
41
+ [self.view addSubview:table];
42
+ table.dataSource = self;
43
+ table.delegate = self;
44
+ [table registerClass:UITableViewCell.class forCellReuseIdentifier:_REUSEID];
45
+
46
+ //m_UserListDict初期化
47
+ m_UserListDict =
48
+ @{
49
+ @"UserCnt": @"3",
50
+ @"0": @{@"name": @"zero"},
51
+ @"1": @{@"name": @"one"},
52
+ @"2": @{@"name": @"two"},
53
+ }.mutableCopy;
54
+ //NSLog(@"%@", m_UserListDict);
55
+
56
+ //hoge初期化
57
+ [self DisplayList];
58
+ }
59
+
60
+ //セル数を返す
61
+ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
62
+ {
63
+ return self.hoge.count;
64
+ }
65
+
66
+ //セル生成
67
+ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
68
+ {
69
+ //セル取得
70
+ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_REUSEID];
71
+ //セル設定
72
+ cell.textLabel.text = self.hoge[indexPath.row];
73
+
74
+ return cell;
75
+ }
76
+
77
+ //セルタップ時に呼ばれる
78
+ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
79
+ {
80
+ //m_UserListDictに追加 (この中はあまり気にしないで下さい)
81
+ {
82
+ //key生成
83
+ NSString *key = @(self.hoge.count).stringValue;
84
+ //obj生成
85
+ NSNumberFormatter *nf = NSNumberFormatter.new;
86
+ nf.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
87
+ nf.numberStyle = NSNumberFormatterSpellOutStyle;
88
+ NSString *name = [nf stringFromNumber:@(self.hoge.count)];
89
+ m_UserListDict[key] = @{@"name": name};
90
+ //UserCnt+1
91
+ m_UserListDict[@"UserCnt"] = @([m_UserListDict[@"UserCnt"] integerValue] + 1).stringValue;
92
+ }
93
+
94
+ //更新
95
+ [self DisplayList];
96
+ [tableView reloadData];
97
+ }
98
+
99
+ //hoge更新
100
+ // reloadDataを外に出した以外は変更無しです
101
+ -(void)DisplayList
102
+ {
103
+ str = [[NSString alloc]init];
104
+ val = [[NSMutableArray alloc]init];
105
+ self.hoge = [[NSMutableArray alloc]init];
106
+
107
+ UserCnt = [m_UserListDict objectForKey:@"UserCnt"];
108
+ iUserCnt = [UserCnt intValue];
109
+ NSLog(@"UserCnt=%zd", iUserCnt);
110
+
111
+ for ( int i = 0; i < iUserCnt; i++){
112
+
113
+ str = [NSString stringWithFormat:@"%d", i];
114
+ val = [[m_UserListDict objectForKey:str] objectForKey:@"name"];
115
+ [self.hoge addObject:val];
116
+ }
117
+ }
118
+
119
+ - (void)didReceiveMemoryWarning
120
+ {
121
+ [super didReceiveMemoryWarning];
122
+ }
123
+
124
+ @end
125
+
126
+ ```