2010-04-16 6 views
0

J'ai stocké des valeurs dans NsMutableDictionaries. Ensuite, j'ai stocké tous les dictionnaires dans NSMutable Array. J'ai besoin d'accéder aux valeurs? Comment puis je faire ça ?Comment accéder aux valeurs stockées dans NSMutableArray de NSMutableDictionary?

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

Je l'ai fait ci-dessus mais il ne fonctionne pas. Je sais que l'erreur est sur les valeurs d'accès. mais, je ne pouvais pas savoir comment le faire? Merci.

+0

ne sais pas pourquoi certains Noob vous feuilletait vers le bas, alors je vous fait un pied jusqu'à annuler IT out. – Jacksonkr

Répondre

2

Vous n'avez pas du tout stocké les dictionnaires, juste les chaînes "dict1", "dict2", "dict3", etc. Le initialiseur de tableau que vous utilisez doit être quelque chose comme

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

Je ne sais pas pourquoi vous avez toutes les [cells addObject:dictionaryN]; lignes commentées, parce que c'est la bonne façon d'ajouter les dictionnaires au tableau; vous devez également avoir un [dictionaryN release]; après chacun d'eux pour éviter les fuites de mémoire.

Pour obtenir les valeurs hors des dictionnaires dans le tableau, vous devez faire quelque chose comme ceci dans votre méthode -tableView:cellForRowAtIndexPath::

NSDictionary *rowDictionary = [cells objectAtIndex:indexPath.row]; 
[(UILabel *)[cell viewWithTag:tag1] setText:[rowDictionary objectForKey:@"date"]]; 
[(UILabel *)[cell viewWithTag:tag2] setText:[rowDictionary objectForKey:@"time"]]; 
// etc. 
+0

Merci. Je suis un peu confus. Donc, je les ai commentés. Merci d'avoir regardé mon code et de pointer mes erreurs. –

Questions connexes