0

事情是。我想将每个对象显示到 tableview 而不是它在 subs 下,所以在这种情况下。123. 我似乎不知道如何显示那一项……也。它现在保存 plist 的方式会覆盖它看起来的 plist。因为每次我去添加它都是新创建的,所以我在网络上关注了数字折磨,这里是堆栈溢出。但似乎没有一个接近我想做的事情。

列表

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;

}

添加服务器视图控制器.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);
}

}

4

2 回答 2

1

每个字典都有 allKeys 方法,该方法返回一个包含...所有键的数组。如果你按照彼得所说的去做,然后你可以用 allKeys 填充你的 tableView

于 2013-11-12T18:08:11.477 回答
0

如果我误读了您的问题,请原谅我,但在重新创建您的 plist 后完成了以下操作:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Sample List" ofType:@"plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:path];

NSLog(@"Plist dict %@", [plistDict objectForKey:@"123"]);

那将输出:

Plist dict {
    Hostname = 123;
    Password = 123;
    Port = 123;
    Username = 123;
}

那是你要找的吗?如果没有,我需要更多关于您要查找的内容的详细信息。

于 2013-11-12T17:56:46.633 回答