が、ちょっと行き詰まりましたので御教授願いたいです。
とあるviewからplistをセーブデータとして保存して、別のViewに表示させようとしています。(Result表示)
保存先のplistには追記する形で保存しており、以下のような形式になっています。
<dict>
<key>data1</key>
<array>
<string>str1</string>
<string>str2</string>
<string>str3</string>
<key>data2</key>
<array>
<string>str1</string>
<string>str2</string>
<string>str3</string>
・・以下<key>から追記
と保存する度に追記していきます。それをTableViewでグループ化表示を行うようにしたのですが、
最初からあるkeyの数だけのグループ化はできるのですが、追記分が反映されません。
元ファイルのplistにはちゃんと追加されているので、アプリを再起動すると追加した分のkeyも出てくるようになります。
最初はViewDidLoadで指定していたのでアプリの初回起動だけしか反映されないのは気がついたのですが
viewが切り替わる度に読み込むメソッドに入れても結果は同じでした。
以下がコードになります。
(void)viewWillAppear:(BOOL)animated
{
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[docPaths objectAtIndex:0] stringByAppendingPathComponent:@"save.plist"];
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
self.names = dict;
NSLog(@"%@",dict);
[dict release];
NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
self.keys = array;
NSLog(@"%@",array);
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [keys count];
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
return [nameSection count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier ];
if (cell == nil){
cell = [[[UITableViewCell alloc]initWithFrame:CGRectZero
reuseIdentifier: SectionsTableIdentifier ] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
-(NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section];
return key;
}