views:

470

answers:

1

I have stored values in NsMutableDictionaries . ThenI stored all the dictionaries in NSMutable Array. I need to access the values ? How can I do that ?

-(void)viewDidLoad 
{
    [super viewDidLoad];

 self.title = @"Library";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:@selector(close:)];
    cells = [[NSMutableArray alloc] initWithObjects:@"dict1", @"dict2", @"dict3", @"dict4", @"dict5", @"dict6", nil];

    dict1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Mon, 01 Feb #2", @"date", @"0.7", @"time", @"1.2MB", @"size", @"200*200", @"pix", nil];
    dict2 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Wed, 02 Mar #3", @"date", @"1.2", @"time", @"2.2MB", @"size", @"300*300", @"pix", nil];
    dict3 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Tue, 03 Apr #5", @"date", @"1.7", @"time", @"2.5MB", @"size", @"240*240", @"pix", nil];
    dict4 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Mon, 01 Feb #2", @"date", @"0.7", @"time", @"1.2MB", @"size", @"200*200", @"pix", nil];
    dict5 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Mon, 10 Nov #5", @"date", @"2.7", @"time", @"4.2MB", @"size", @"200*400", @"pix", nil];
    dict6 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Mon, 11 Dec #6", @"date", @"4.7", @"time", @"2.2MB", @"size", @"500*200", @"pix", nil];

    //[cells addObject:dict1];
    //[cells addObject:dict2];
    //[cells addObject:dict3];
    //[cells addObject:dict4];
    //[cells addObject:dict5];
    //[cells addObject:dict6];
}  

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return [cells count];

}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{   cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    //cell.contentView.frame = CGRectMake(0.0f, 0.0f, 320.0f, 80.0f);
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    UIImageView *image1 = [[UIImageView alloc]init];
    image1.frame = CGRectMake(0.0f, 0.0f, 80.0f, 80.0f);
    image1.tag = tag7;

    UILabel *dateLabel = [[UILabel alloc]init];
    dateLabel.frame = CGRectMake(100.0f, 5.0f, 120.0f, 25.0f);
    dateLabel.font = [UIFont fontWithName:@"Georgia" size:10];
    dateLabel.tag = tag1;

    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.frame = CGRectMake(100.0f, 30.0f, 40.0f, 25.0f);
    timeLabel.font = [UIFont fontWithName:@"Georgia" size:10];
    timeLabel.tag = tag2;

    UILabel *sizeLabel = [[UILabel alloc] init];
    sizeLabel.frame = CGRectMake(160.0f, 30.0f, 40.0f, 25.0f);
    sizeLabel.font = [UIFont fontWithName:@"Georgia" size:10];
    sizeLabel.tag = tag3;

    UILabel *pixLabel = [[UILabel alloc] init];
    pixLabel.frame = CGRectMake(220.0f, 30.0f, 40.0f, 25.0f);
    pixLabel.font = [UIFont fontWithName:@"Georgia" size:10];
    pixLabel.tag = tag4;

    UILabel *shareLabel = [[UILabel alloc] init];
    shareLabel.frame = CGRectMake(100.0f, 55.0f, 100.0f, 25.0f);
    shareLabel.font = [UIFont fontWithName:@"Georgia" size:10];
    shareLabel.tag = tag5;

    UILabel *deleteLabel = [[UILabel alloc] init];
    deleteLabel.frame = CGRectMake(220.0f, 55.0f, 100.0f, 25.0f);
    deleteLabel.font = [UIFont fontWithName:@"Georgia" size:10];
    deleteLabel.tag = tag6;

    [cell.contentView addSubview:dateLabel];
    [cell.contentView addSubview:timeLabel];
    [cell.contentView addSubview:sizeLabel];
    [cell.contentView addSubview:pixLabel];
    [cell.contentView addSubview:shareLabel];
    [cell.contentView addSubview:deleteLabel];
    [cell.contentView addSubview:image1];

    [dateLabel release];
    [timeLabel release];
    [sizeLabel release];
    [pixLabel release];
    [shareLabel release];
    [deleteLabel release];
    [image1 release];
}

// Set up the cell...

[(UILabel *)[cell viewWithTag:tag1] setText:[cells objectAtIndex:[dict1 objectForKey: @"date"]]];
[(UILabel *)[cell viewWithTag:tag2] setText:[cells objectAtIndex:[dict1 objectForKey: @"time"]]];
[(UILabel *)[cell viewWithTag:tag3] setText:[cells objectAtIndex:[dict1 objectForKey: @"size"]]];
[(UILabel *)[cell viewWithTag:tag4] setText:[cells objectAtIndex:[dict1 objectForKey: @"pix"]]];
[(UILabel *)[cell viewWithTag:tag5] setText:@"Share"];
[(UILabel *)[cell viewWithTag:tag6] setText:@"Delete"];
 cell.imageView.image = [UIImage imageNamed:@"image2.png"];

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80.0f;
}

I did in above way but it is not working. I know the mistake is at the accessing values. but, I could not get how to do it ? Thank You.

+2  A: 

You haven't stored the dictionaries at all—just the strings "dict1", "dict2", "dict3", and so on. The array initializer you're using should be something like

cells = [[NSMutableArray alloc] initWithCapacity:6];

I'm not sure why you've got all of the [cells addObject:dictionaryN]; lines commented out, because that's the correct way to add the dictionaries to the array; you also need to have a [dictionaryN release]; after each of them to prevent memory leaks.

To get the values out of the dictionaries in the array, you need to do something like this in your -tableView:cellForRowAtIndexPath: method:

NSDictionary *rowDictionary = [cells objectAtIndex:indexPath.row];
[(UILabel *)[cell viewWithTag:tag1] setText:[rowDictionary objectForKey:@"date"]];
[(UILabel *)[cell viewWithTag:tag2] setText:[rowDictionary objectForKey:@"time"]];
// etc.
Noah Witherspoon
Thank you. I am bit confused. So, I commented them. Thank you for looking my code and pointing my mistakes.
srikanth rongali