Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将Plist显示到UITableview中_Iphone_Ios_Uitableview_Plist - Fatal编程技术网

Iphone 将Plist显示到UITableview中

Iphone 将Plist显示到UITableview中,iphone,ios,uitableview,plist,Iphone,Ios,Uitableview,Plist,我想将Plist显示到UITableView中。通过下面的代码,我可以显示一个键值,即状态列表。但是我想显示三个表的所有值 - (id)readPlist:(NSString *)fileName { NSData *plistData; NSString *error; NSPropertyListFormat format; id plist; NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileNam

我想将
Plist
显示到
UITableView
中。通过下面的代码,我可以显示一个键值,即
状态列表
。但是我想显示三个表的所有值

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


也可以用
值显示
名称

只需尝试在
NSDictionary
NSArray
中获取数据,然后在
UITableView

NSString *file = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file]; 
并使用tableValue及其名称,如bellow

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

我希望这对您有所帮助。

也许可以使用表格部分

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

编辑:更正了一些打字错误

想法

  • 在听写器中加载plist
  • 可选:对其进行排序(!)
  • 为每个部分创建数据数组
  • 创建节的数组
  • 然后实现以下内容

  • viewDid load->在字典中加载plist文件,创建一个节数组和一个数据字典

  • NumberOfSectionsTableView->节数。在你的情况下,钥匙的数量

  • numberOfRowsInSection->每个节的行数

  • titleForHeaderInSection->对于每个节,节的标题

  • cellForRowAtIndexPath->所有行的数据

  • sectionIndexTitlesForTableView->保存所有节名的数组。在你的情况下,所有的钥匙

  • 假设您有一个名为mySections的属性和一个名为myData的属性 还假设您使用带有回收标识符“cell”的标准单元的故事板


    你到底想要什么?每个表的值中有三个部分,或者是三个表的一个负号?如果您能显示readPlist:的代码和当前得到的输出,那会有所帮助。@Zaphod抱歉,这不是错误。但我只能显示listofstates的键值,即雪兰莪州、丁加奴州、gfh州、,柔佛和马六甲only@SameSungVsIphone我认为JV360的答案可能就是你想要的。但是我确实看到您使用了
    propertyListFromData:mutabilityOption:format:errorDescription:
    ,这已经被弃用了。苹果建议使用
    propertyListWithData:options:format:error:
    @jv360的解决方案最容易列出这三个表。:-)希望能有所帮助。也许有一些打字错误。如果您有问题,请提问。您的代码不适用于我…:(我第一次尝试在Plist中编写代码…无法理解您的代码…mycode是什么?它应该是一个通用代码;-)您收到了什么错误消息?感谢您的工作。但当我将其向下点击时,它会崩溃。我无法在此中添加滚动视图..如何操作?您使用情节提要吗?如果是,只需添加tableview控制器。直接创建tableview控制器,而不是添加表视图的viewcontroller。滚动自动出现在tableview控制器中,无需添加内容。当您滚动以帮助您时,可能会向我显示您收到的错误消息。
       @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;
    }