Plist进入UItableview

时间:2012-12-18 05:35:39

标签: iphone ios uitableview ios5 plist

  

可能重复:
  Displaying Plist data into UItableview

我有一个带字典的plist和每个字典的字符串数量。显示在下面的url中。这个项目列表在plist.it给我输出但是...没有正确显示..我认为问题在CellForRowatindexPath,如果NSLOG valueArray输出正确

我需要将这些plist数据显示在UItableview

eneter image

怎么做?

我的代码:

- (void)viewWillAppear:(BOOL)animated
{
    // get paths from root direcory
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                 NSUserDomainMask, YES);
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"p.plist"];

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];

    valueArray = [dict objectForKey:@"title"];

    self.mySections=[valueArray copy];
    NSLog(@"value array %@",self.mySections);

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.mySections count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSString *key = [[self.mySections objectAtIndex:section]objectForKey:@"pass"];
    return [NSString stringWithFormat:@"%@", key];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.mySections count];
}

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

    static NSString *CellIdentifier = @"Cell";

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

    // Configure the cell...
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    cell.textLabel.text = [[self.mySections objectAtIndex:row] objectForKey:@"title"];
    cell.detailTextLabel.text=[[self.mySections objectAtIndex:section] objectForKey:[allKeys objectAtIndex:1]];

    return cell;
}

2 个答案:

答案 0 :(得分:1)

这里正确设置numberOfRowsInSection方法的行数正确..

此处也使用row代替section ..并将objectForKey设置为字符串值..

<强>更新

cell.textLabel.text = @"protein,carbs,fats,title";
cell.detailTextLabel.text=[NSString stringWithFormat:@"%@,%@,%@,%@",[[self.mySections objectAtIndex:row] objectForKey:@"protein"],[[self.mySections objectAtIndex:row] objectForKey:@"carbs"],[[self.mySections objectAtIndex:row] objectForKey:@"fats"],[[self.mySections objectAtIndex:row] objectForKey:@"title"]];

我希望这可以帮助你...

答案 1 :(得分:1)

试试这个,我想这就是你想要的:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.mySections count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSString *key = [[self.mySections objectAtIndex:section]objectForKey:@"pass"];
    return [NSString stringWithFormat:@"%@", key];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[[self.mySections objectAtIndex:section ] allKeys] count] ;
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    NSDictionary *dict = [self.mySections objectAtIndex:indexPath.section];
    cell.textLabel.text = [dict.allKeys objectAtIndex:indexPath.row];
    cell.detailTextLabel.text= [dict valueForKey:[dict.allKeys objectAtIndex:indexPath.row]];

    return cell;
}