Display Plist into UITableview

2019-04-12 04:48发布

I want to display Plist to the UITableView .By my below code I can display one key values that is list of states. but i want to display all the values of three tables.

- (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;}

MY output comes only listofkeystates values

enter the image is it also possible to display key name with values

3条回答
Ridiculous、
2楼-- · 2019-04-12 05:04

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;
}
查看更多
爷、活的狠高调
3楼-- · 2019-04-12 05:13

Maybe use table sections...

-(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;
}
查看更多
地球回转人心会变
4楼-- · 2019-04-12 05:23

just try to get data in NSDictionary and NSArray and then use it in UITableView

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

and use tableValue with its name like bellow...

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

i hope this help you..

查看更多
登录 后发表回答