如何编写字典对象的plist数组..动态地填充tableview

时间:2011-08-10 13:05:25

标签: iphone ios4 iphone-sdk-3.0

plist合作,我是新手。 我想保存两种类型的值,这两种是标签,即

label1 label2

所以为此我尝试编写代码,但只能保存一个 在plist中标记label1 ..然后将其填充到表中,这样我就可以得到一个值 cell.textlabel.text = label1.text

但我也需要这个..

cell.detailedlabel.text = label2.text

以下是我的代码..保存label1.text

-(IBAction) myBrand:(id) sender
{
     NSLog(@"mylist  Clicked");    
    NSMutableArray *array = [[NSMutableArray alloc] init];

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,      NSUserDomainMask, YES);

NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];    

    NSString *plistPath = [docsDir stringByAppendingPathComponent:@"Data.plist"];


    [array addObjectsFromArray:[NSArray arrayWithContentsOfFile:plistPath]];
    [array addObject:searchLabel.text];

    [array writeToFile:plistPath atomically: TRUE];    
}

现在,如果我想在阵列中再保存一个标签..并在细胞上填充详细信息。我将继续操作..请帮助我...

嗨朋友们..我想要这样的事情......

NSMutableArray *array = [[NSMutableArray alloc] init];


    // get paths from root direcory

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);

    // get documents path



     NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];    // get the path to our Data/plist file
        NSString *plistPath = [docsDir stringByAppendingPathComponent:@"Data.plist"];

        //This copies objects of plist to array if there is one
        [array addObjectsFromArray:[NSArray arrayWithContentsOfFile:plistPath]];
        //[array addObject:entity];
        [array insertObject:entity atIndex:0];
       [array insertObject:category atIndex:0];

        NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: entity, category, nil] forKeys:[NSArray arrayWithObjects: @"Entity", @"Category", nil]];   

此致 兰吉特

1 个答案:

答案 0 :(得分:2)

浏览下面的代码部分,这将解决在plist中存储两个标签值的问题......

NSMutableArray *data = [[NSMutableArray alloc] init];

NSMutableDictionary* newDict = [[NSMutableDictionary alloc] init];
[newDict setValue:label1.text forKey:labell];    //forKey identify the name of the array element in dictionary for plist
[newDict setValue:label2.text forKey:label2];

[data addObject:newDrink];
[newDict release];