简体   繁体   English

在UITableView中查看plist数据

[英]View plist data in UITableView

The thing is. 事情是。 i want to display the each object to the tableview not it's under subs so in this case. 我想在tableview中显示每个对象,而不是在subs下,因此在这种情况下。 123. i can't seem to figured out how to display the that one item... also. 123.我似乎还没弄清楚如何显示那个项目。 the way it saves the plist now it overwrites the plist it seems. 现在它保存plist的方式将覆盖看上去的plist。 since it's fresh created each time i go add i followed numbers torturials on the web and here on stack overflow. 因为它是我每次添加时都是新鲜创建的,所以我在网络上跟随着数字折磨,这里是堆栈溢出。 but none seem to get close to what i want to do. 但是似乎没有一个人比我想做的要近。

plist中

MainTableViewController.h MainTableViewController.h

{
NSMutableDictionary *arrayD;
}

MainTableViewController.m 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 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);
}

} }

Each dictionary has allKeys method that returns an array with... all keys. 每个字典都有allKeys方法,该方法返回包含所有键的数组。 If you do what Peter said and then you can just populate your tableView with allKeys 如果您按照彼得说的去做,则可以使用allKeys填充tableView

Forgive me if I misread your question but the following was done after recreating your plist: 如果我读错了您的问题,请原谅我,但是在重新创建您的plist之后,以下操作已完成:

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

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

That would output: 将会输出:

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

Is that what you were looking for? 那是您要找的东西吗? If not, ill need a bit more detail with regards to what it is that you're looking for. 如果不是这样,生病的人需要更多有关您正在寻找的东西的细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM