【问题标题】:Plist into UItableview [duplicate]Plist到UItableview [重复]
【发布时间】:2012-12-18 05:35:38
【问题描述】:

可能重复:
Displaying Plist data into UItableview

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

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

.

如何做到这一点?

我的代码:

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

【问题讨论】:

  • 如果您认为问题出在cellForRowAtIndexPath,请设置断点并单步执行代码并查看错误值出现的位置。或者至少使用 NSLog 并找出答案。
  • 查看 mySections 数组的打印输出而不是 plist 会更有用。还有,怎么了?您没有描述问题——“没有正确显示”信息量不是很大。
  • 你是否正确设置了 UITableViewDataSource
  • 另外,你真的要返回 self.mySections.count 的节数和行数吗?
  • @rdelmar if i nslog { value array ( { pass = item; title = item; }, { Protein = ""; carbs = ""; fats = ""; pass = ""; title = ""; }, { 蛋白质 = 7; 碳水化合物 = 7; 脂肪 = 7; pass = 糖; 标题 = 10; }, { pass = hi; title = hii; })

标签: iphone ios uitableview ios5 plist


【解决方案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;
}

【讨论】:

  • 男人 [[self.mySections[section],dict.allKeys[indexPath.row];给我一些错误-> subscript require size of interface "NSARRAY" which is not constant in non-fragile ABI...这是什么??
  • [[self.mySections[section],dict.allKeys[indexPath.row] 这是什么?我的代码中没有这样的行。我测试了这段代码,对我来说效果很好。
  • -> return [[self.mySections[section] allKeys] count]; 给我错误->subscript require size of interface "NSARRAY" which is not constant in non-fragile ABI
  • cell.textLabel.text = dict.allKeys[indexPath.row]; cell.detailTextLabel.text= [dict valueForKey:dict.allKeys[indexPath.row]]; ..这里也有同样的错误
  • @Christien,我相信一定有办法,但我现在想不起来——我累了,该睡觉了。
【解决方案2】:

在这里正确设置 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"]];

希望对你有帮助……

【讨论】:

  • 输出不正确
  • @Christien 你在 allKeys 中得到了什么输出??
  • if i nslog { value array ( { pass = item; title = item; }, { Protein = ""; carbs = ""; fats = ""; pass = ""; title = " "; }, { 蛋白质 = 7; 碳水化合物 = 7; 脂肪 = 7; pass = 糖; title = 10; }, { pass = hi; title = hii; } ) ....它是正确的
  • 我认为你的标题是正确的,但不是详细标签,对吧???
  • 使用 valueForKey insted of objectFor key IF my.sections 是 NSMutableArray 试试这个 [self.mySections objectAtIndex:indexPath.row]valueForKey:@"pass"];
猜你喜欢
  • 1970-01-01
  • 2012-06-24
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多