How to get NSDictionary data in a UITableView

I have 2 NSArrays that I am trying to bind together in an NSDictionary, so that each server_name has a providerId associated with it.

I expected the two arrays to be linked together using self.providersDict = [NSDictionary dictionaryWithObjects:providerName forKeys:providerId];and then the name of the provider, which will appear as the heading of the table cell, and the identifier as the subheading of the table cell.

At the moment, I cannot get the data displayed in a UITable or even exit the dictionary using:

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

declared in .h:

NSArray *providerName;
NSArray *providerId;

@property (nonatomic, strong) NSDictionary *providersDict;

and in viewDidLoad:

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

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

UITable methods:

-(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.

+5
source share
2 answers
NSString *providerNameString = [self.providersDict objectForKey:@"provider"];
NSString *providerIdString = [self.providersDict objectForKey:@"object"];

self.providersDict 1, 2, 3. , 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;

[tableView reloadData]; self.providersDict.

, Provider , . .

+16

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

All Articles