【问题标题】:Help w/ array of dictionaries in a tableview帮助 w/ 表格视图中的字典数组
【发布时间】:2010-07-29 17:59:21
【问题描述】:

这有点令人困惑,所以请多多包涵。

我有一个字典数组 (listOfStates)。其中 listOfStates 具有这种结构:

{stateName} - {stateCapital}
{Alabama} - {Montgomery}
{Alaska} - {Juneau}
{Arizona} - {Phoenix}
...
{West Virginia} - {Charleston}
{Wisconsin} - {Madison}
{Wyoming} - {Cheyenne}

这是用于获取每个美国州 (stateName) 的第一个字母以便按字母顺序将它们组合在一起的代码:

listOfStates = [[NSArray alloc] init];  
stateIndex = [[NSMutableArray alloc] init];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];

for (NSDictionary *row in listOfStates) {
    [tempArray addObject:[row valueForKey:@"stateName"]];

     for (int i=0; i<[tempArray count]-1; i++){
     char alphabet = [[tempArray objectAtIndex:i] characterAtIndex:0];
     NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet];
         if (![stateIndex containsObject:uniChar]){  
             [stateIndex addObject:uniChar];
         }
     }
}

该代码运行良好,但是在我使用 NSPredicate对数组进行排序。

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
    //---get the letter in the current section---
NSString *alphabet = [stateIndex objectAtIndex:[indexPath section]];

    //---get all states beginning with the letter---
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSArray *states = [[listOfStates valueForKey:@"stateName"] filteredArrayUsingPredicate:predicate];

        //---extract the relevant state from the states object---
    cell.textLabel.text = [states objectAtIndex:indexPath.row];
        //cell.textLabel.text = [[listOfStates objectAtIndex:indexPath.row] objectForKey:@"stateName"];
        //cell.detailTextLabel.text = [[listOfStates objectAtIndex:indexPath.row] objectForKey:@"stateCapital"];

return cell;
}

感谢任何帮助,如果您认为它会帮助您理解,我非常愿意发送整个项目。

非常感谢。

【问题讨论】:

  • i flow ur ans 但同样的问题是让我。我打印名字和姓氏但只有名字是显示姓氏不显示做什么请帮助我。

标签: iphone uitableview nsarray nsdictionary nspredicate


【解决方案1】:
cell.textLabel.text = [[states objectAtIndex:indexPath.row] objectForKey:@"stateName"];
cell.detailTextLabel.text =[states objectAtIndex:indexPath.row] objectForKey:@"stateCapital"];

更新:

不幸的是,只有 'states' 数组 包括州名...

将谓词和过滤器更改为:

NSPredicate *p=[NSPredicate predicateWithFormat:@"stateName beginswith[c] %@", alphabet];
NSArray *states=[listOfStates filteredArrayUsingPredicate:p];

...这将为您提供所有以相同字母开头的状态数组。然后您可以对数组进行排序,该行将为您提供正确的状态字典。

【讨论】:

  • 令我懊恼的是,这并不能正常工作。这是我尝试的第一件事。
  • 不幸的是,'states' 数组只包含状态名称,当 NSPredicate 创建数组时,会删除大写字母。
【解决方案2】:

也许改变数据结构,这样你就有一个字典,其中州名作为键,州首府作为值。这样您就可以对键进行排序并使用键而不是索引来获取值。

【讨论】:

  • 不确定如何执行此操作,因为初始数组是通过 SQLite 查询生成的。关于如何做的建议? wufpakjack@yahoo.com
  • + [NSDictionary dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys]; 应该可以。
猜你喜欢
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多