简体   繁体   English

如何将NSDictionary数据放入UITableView

[英]How to get NSDictionary data into a UITableView

I have 2 NSArrays that I'm trying to link together in an NSDictionary so that each providerName has a providerId associated with it. 我想在NSDictionary中将2个NSArray链接在一起,以便每个providerName都有一个与其关联的providerId

I expected the two arrays to be associated with each other using self.providersDict = [NSDictionary dictionaryWithObjects:providerName forKeys:providerId]; 我期望使用self.providersDict = [NSDictionary dictionaryWithObjects:providerName forKeys:providerId];将两个数组相互关联self.providersDict = [NSDictionary dictionaryWithObjects:providerName forKeys:providerId]; and then the providerName to be displayed as the table cell title and the id as the table cell subtitle. 然后将providerName显示为表格单元格标题,将id显示为表格单元格字幕。

At the moment I can't get the data displayed on the UITable or even log out the dictionary using: 目前,我无法获得显示在UITable上的数据,甚至无法使用以下方法注销字典:

   for (id key in self.providersDict) {
    NSLog(@"key: %@, value: %@", key, [self.providersDict objectForKey:key]);
}

declared in the .h: 在.h中声明:

NSArray *providerName;
NSArray *providerId;

@property (nonatomic, strong) NSDictionary *providersDict;

and in the viewDidLoad: 并在viewDidLoad中:

    providerName = [NSArray arrayWithObjects:@"provider1", @"provider2", @"provider3", nil];
    providerId = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];

    self.providersDict = [NSDictionary dictionaryWithObjects:providerName
                                                           forKeys:providerId];

the UITable methods: UITable方法:

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


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



 static NSString *unifiedID = @"aCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];

}

for (id key in self.providersDict) {
    NSLog(@"key: %@, value: %@", key, [self.providersDict objectForKey:key]);
}

NSString *providerNameString = [self.providersDict objectForKey:@"provider"];
NSString *providerIdString = [self.providersDict objectForKey:@"object"];
cell.textLabel.text  = providerNameString;
cell.detailTextLabel.text  = providerIdString;


return cell;

}

thanks for the help. 谢谢您的帮助。

NSString *providerNameString = [self.providersDict objectForKey:@"provider"];
NSString *providerIdString = [self.providersDict objectForKey:@"object"];

You have initialized self.providersDict with keys 1, 2, 3. So you should Try to fetch providerNameString using those keys and not @"provider" same for providerIdString . 你已经初始化按键1,2,3 self.providersDict所以你应该尝试获取providerNameString使用这些按键,而不是@"provider"同为providerIdString

Try 尝试

NSString *key = [self.providersDict allKeys][indexPath.row];

NSString *providerNameString = self.providersDict[key];
NSString *providerIdString = key;
cell.textLabel.text  = providerNameString;
cell.detailTextLabel.text  = providerIdString;

And don't forget to call [tableView reloadData]; 并且不要忘了调用[tableView reloadData]; once your self.providersDict is initialized. 一旦您的self.providersDict被初始化。

As for a suggestion why dont you create a Model Class for Provider with id, name and other information. 至于建议,为什么不使用ID,名称和其他信息为Provider创建模型类。 You can add each provider in an array and would make your life a lot easier. 您可以将每个提供程序添加到一个数组中,这将使您的生活更加轻松。

For your purpose , you don't need any NSDictionary , simply use two NSArrays to populate the data in UITableView 为了您的目的,您不需要任何NSDictionary ,只需使用两个NSArrays填充UITableView的数据

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *unifiedID = @"aCellID";
    UITableViewCell *cell = 
    [tableView dequeueReusableCellWithIdentifier:unifiedID];

    if (!cell) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];  
    }

    NSString *providerNameString = [providerName objectAtIndex:indexPath.row];
    NSString *providerIdString = [providerId objectAtIndex:indexPath.row];
    cell.textLabel.text  = providerNameString;
    cell.detailTextLabel.text  = providerIdString;
    return cell;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM