事はです。各オブジェクトをサブの下ではなくテーブルビューに表示したいので、この場合。123. そのアイテムを表示する方法がわかりません... また。plist を保存する方法により、plist が上書きされるようになりました。追加するたびに作成されたばかりなので、Web上とここではスタックオーバーフローで数字の拷問を受けました。しかし、私がやりたいことに近づくものはないようです。
MainTableViewController.h
{
NSMutableDictionary *arrayD;
}
MainTableViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Property List.plist code
//Gets paths from root direcory.
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
//Get documents path.
NSString *documentsPath = [paths objectAtIndex:0];
//Get the path to our PList file.
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Servers.plist"];
//Check to see if Property List.plist exists in documents.
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
[[NSFileManager defaultManager]copyItemAtPath:[[NSBundle mainBundle]pathForResource:@"Servers" ofType:@"plist"] toPath:plistPath error:nil];
//If not in documents, get property list from main bundle.
// plistPath = [[NSBundle mainBundle] pathForResource:@"Servers" ofType:@"plist"];
}
//Read property list into memory as an NSData object.
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
//convert static property list into dictionary object.
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp){
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
//Display values.
NSMutableDictionary *dictinoary =[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
tableData = [dictinoary objectForKey:@"Hostname"];
NSLog(@"Count: %i", [arrayD count]);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Hostname";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
AddServerViewController.m
- (IBAction)SaveData:(id)sender {
//Get paths from root direcory.
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
//Get documents path.
NSString *documentsPath = [paths objectAtIndex:0];
//Get the path to our PList file.
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Servers.plist"];
//Set the variables to the values in the text fields.
hostName = hostEntered.text;
portNumber = portEntered.text;
userName = userNameEntered.text;
passWord = passWordEntered.text;
NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4];
NSMutableDictionary *data;
data = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects: hostName, portNumber, userName, passWord, nil] forKeys:[NSArray arrayWithObjects:@"Hostname", @"Port", @"Username", @"Password", nil]];
[rootObj setObject:data forKey:hostName];
NSString *error = nil;
//Create NSData from dictionary.
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
//Check is plistData exists.
if(plistData){
//Write plistData to our Properity List.plist file.
[plistData writeToFile:plistPath atomically:YES];
}
else{
NSLog(@"Error in saveData: %@", error);
}
}