Displaying Array and Dictionary values in a tableview

10,185

Solution 1

If you try to assign the some text to the cell's textlabel the object returned from:

[mArray objectAtIndex:indexPath.row];

should be a string and not a whole dictionary.

When you just put the whole dictionary in the array [mArray objectAtIndex:indexPath.row]; will return a dictionary and you cannot assign a dictionary to the textlabel so you get an exception. If you want to store the whole dictionary use objectForKey to get the string you want:

cell.textLabel.text = [[mArray objectAtIndex:indexPath.row] objectForKey:@"WHAT_EVER_KEY_YOU_WANT"];

btw, you are not using ARC zo you probably want to get an autoreleased UITableViewCell so add autorelease to you cell:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];

or

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
[cell autorelease];

Solution 2

Declare an array in .h file:

NSArray *keys

write this in viewDidLoad

keys = [mDictionary allKeys];

This in cellForRowAtIndexPath

cell.textLabel.text = [mDictionary objectForKey:[keys objectAtIndex:indexPath.row];
Share:
10,185
Newbee
Author by

Newbee

working as Software Developer

Updated on June 04, 2022

Comments

  • Newbee
    Newbee almost 2 years

    I have created an array, two dictionaries and a tableview. The tableview should display the contents of array and that array contains the data of dictionaries. When i try to display it using "[mArray addObject:(mDictionary)];" its throwing an exception but if i use "[mArray addObject:[mDictionary objectForKey:@"firstname"]];" its working fine.

    Can you help me to understand why is it happening and isn't it possible to display the dictionary value without using "objectForKey"?

    Here is my "ViewController.m".

    #import "ViewController.h"
    
    @implementation ViewController
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        mArray = [[NSMutableArray alloc]init];
        mDictionary = [[NSMutableDictionary alloc]init];
        [mDictionary setValue:@"shanu" forKey:@"firstname"];
        [mDictionary setValue:@"kumar" forKey:@"lastname"];
        mDictionary2 = [[NSMutableDictionary alloc]init];
        [mDictionary2 setValue:@"hgjghjgf" forKey:@"firstname1"];
        [mDictionary2 setValue:@"ghjgjgfj" forKey:@"lastname1"];
        [mArray addObject:mDictionary];
        [mArray addObject:mDictionary2];
    
        CGRect frame = self.view.frame;
        UITableView *tableView = [[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain];
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        tableView.backgroundColor = [UIColor cyanColor];
        tableView.separatorColor = [UIColor blackColor];
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.view addSubview:tableView];
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return @"Array and Tableview";
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        return mArray.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.textLabel.text = [mArray objectAtIndex:indexPath.row];
        return cell;
    }
    
    - (void)dealloc
    {
        [mArray release];
        [mDictionary release];
        [mDictionary2 release];
        [super dealloc];
    }
    @end
    
  • Newbee
    Newbee over 11 years
    i used the above code but its displaying only the firstname value.
  • Newbee
    Newbee over 11 years
    that means without using the key values we can not display the dictionary data?
  • Tieme
    Tieme over 11 years
    Yes, and use the objectForKey to get the right NSString object. Well... you could also use the description of the dictionary for the textlabel if that's what you want: [[mArray objectAtIndex:indexPath.row] description];
  • Prasad G
    Prasad G over 11 years
    Change UITableViewCellStyleDefault to UITableViewCellStyleSubtitle and also should change key names are same in all dictionary objects.
  • CRDave
    CRDave over 11 years
    What problem you are facing in this?