如何在UITableview中显示NSDictionary键和值?

时间:2014-08-13 06:55:29

标签: ios objective-c uitableview nsarray nsdictionary

我有这样的NSDictionary: -

aberrancy =     (
    "0Dilution for Infusion.html"
);
accessory =     (
    "0Dilution for Infusion.html"
);
activity =     (
    "0Pharmacology.html"
);
acute =     (
    "0Pharmacology.html"
);
adenosine =     (
    "0Guidelines Precautions.html"
);
administration =     (
    "0Guidelines Precautions.html"
);
aneurysm =     (
    "0Dilution for Infusion.html"
);
arrhythmias =     (
    "0Guidelines Precautions.html"
);
artery =     (
    "0Dilution for Infusion.html"
);
asthma =     (
    "0Guidelines Precautions.html"
);

我想在textlabel的{​​{1}}和detailtextLabel中显示这些键及其值。 请告诉我如何做到这一点我是ios编程的新手。

1 个答案:

答案 0 :(得分:4)

使用以下代码创建字典,然后分别在两个不同的数组中提取键和值。

  NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"1",@"1",@"2",@"2", nil];
  NSArray *keyArray = [dictionary allKeys];
  NSArray *valueArray = [dictionary allValues];

以下是TableView数据源方法示例。这是你应该阅读这些值的方法。

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

  [cell.textLabel setText:[keyArray objectAtIndex:indexPath.row]];
  [cell.detailTextLabel setText:[valueArray objectAtIndex:indexPath.row]];

  return cell;
}