【问题标题】:How to display NSDictionary key and values in UITableview?如何在 UITableview 中显示 NSDictionary 键和值?
【发布时间】:2014-08-13 06:55:28
【问题描述】:

我有这样的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"
);

我想在UITableViewtextlabeldetailtextLabel 中显示这些键及其值。 请告诉我该怎么做,我是 ios 编程新手。

【问题讨论】:

标签: ios objective-c uitableview nsarray nsdictionary


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多