3

我想显示PlistUITableView。通过我下面的代码,我可以显示一个键值,即list of states. 但我想显示三个表的所有值。

- (id)readPlist:(NSString *)fileName 
{
NSData *plistData;
NSString *error;
NSPropertyListFormat format;
id plist;

NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
plistData = [NSData dataWithContentsOfFile:localizedPath]; 

plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
if (!plist) {
NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String],      [error UTF8String]);

}

return plist;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[[self readPlist:@"sample"] objectForKey:@"ListOfStates"] count];

}


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = [[[self readPlist:@"sample"] objectForKey:@"ListOfStates"] objectAtIndex:indexPath.row];

return cell;}

我的输出只有listofkeystates

输入图片 是否也可以显示key名称values

4

3 回答 3

4

也许使用表格部分......

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    NSDictionary* dict = [self readPlist:@"sample"];
    return dict.allKeys.count;
}

-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSDictionary* dict = [self readPlist:@"sample"];
    return [dict.allKeys objectAtIndex:section];
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary* dict = [self readPlist:@"sample"];
    NSString* key = [dict.allKeys objectAtIndex:section];
    return [[dict valueForKey:key] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary* dict = [self readPlist:@"sample"];
    NSString* key = [dict.allKeys objectAtIndex:indexPath.section];
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [[dict objectForKey:key] objectAtIndex:indexPath.row];
    return cell;
}
于 2012-12-05T12:55:13.740 回答
2

EDIT : Corrected some typo mistakes

The idea

  1. Loading plist in a dictonary
  2. optional : sort it (!)
  3. create the array of data for each section
  4. create the array of section

Then implement the following things

  1. viewDid load -> load the plist file in a dictionary, create an array of the sections and a dictionary of the data

  2. numberOfSectionsInTableView -> the number of sections. In your case the number of keys

  3. numberOfRowsInSection -> the number of rows of each section

  4. titleForHeaderInSection -> for each section, the title of the section

  5. cellForRowAtIndexPath -> the data of all row

  6. sectionIndexTitlesForTableView -> the array that holds all you sections names. In your case all the keys

assuming you have a property called mySections and a property called myData also assuming you use storyboard with standard cell with recycle identifier "Cell"

   @synthesize mySections
   @synthesize myData        

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        //LOADING THE PLIST FILE

        //Create a string representing the file path
        NSString *plistPath = [bundle pathForResource:@"yourFilename" ofType:@"plist"];

        //Load the file in a dictionnary
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

        self.myData = dict;

       //SORTING THE DICTIONARY    
       NSArray *dicoArray = [[self.myData allKeys] sortedArrayUsingComparator:^(id firstObject, id secondObject) {
          return [((NSString *)firstObject) compare:((NSString *)secondObject) options: NSCaseInsensitiveSearch];
      }];

      self.mySections = dicoArray;
    }

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
   {
       //Return the number of sections.
       return [self.mySections count];
   }

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
   {
       // Return the number of rows in the section.
       NSString *key = [self.mySections objectAtIndex:section];
       NSArray *dataInSection = [self.myData objectForKey:key];
       return [dataInSection count];
  }

  -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
      NSString *key = [self.mySections objectAtIndex:section];
      return [NSString stringWithFormat:@"%@", key];
    }

  -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        return self.mySections;
   }

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath
 {
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    NSString *key = [self.mySections objectAtIndex:section];
    NSArray *dataForSection = [self.myData objectForKey:key];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = [dataForSection objectAtIndex:row];

    return cell;
}
于 2012-12-05T13:12:45.523 回答
2

只是尝试获取数据NSDictionary然后NSArray使用它UITableView

NSString *file = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file]; 

并使用 tableValue 的名称,如下所示...

NSArray *array = [dict objectForKey:@"ListOfStates"]; 
NSLog(@"%@", array);

我希望这对你有帮助..

于 2012-12-05T12:53:34.303 回答