2011-07-19 8 views
0

私はXcodeのコーディングで初心者です。 私が得たいのは、セクションがあるplistのテーブルビューロードです。 私の問題は、私のセクションに正しく行を配置することです。Xcode:テーブルビューのセクションに関する問題

私の意見では、各エントリのセクションの名前がありますが、これらの情報を取得する方法はわかりません。だから私はコードで私の2つのセクションの名前を手動で追加します。

このように私のplistを見て:

<dict> 
    <key>Root</key> 
    <array> 
     <dict> 
      <key>DESCRIPTION</key> 
      <string>Robe ponctuée de petites tâches sombres plus ou moins rondes et pleines tel que l&apos;on retrouve chez le guépard</string> 
      <key>TITLE</key> 
      <string>Spotted</string> 
      <key>IMAGE</key> 
      <string></string> 
      <key>MINI</key> 
      <string>spotted.png</string> 
      <key>CAT</key> 
      <string>MOTIFS</string> 
     </dict> 
     <dict> 
      <key>DESCRIPTION</key> 
      <string>Robe ponctuée de taches plutôt rondes offrant deux tons contrastants. Le centre de celles-ci se voulant plus clair et le pourtour plus sombre. On appelle rosette ouverte une tâche qui n&apos;est pas totalement cerclée par le pourtour plus foncé et rappelant la forme d&apos;un croissant de lune. </string> 
      <key>TITLE</key> 
      <string>Rosettes ouvertes ou demi-lunes</string> 
      <key>IMAGE</key> 
      <string></string> 
      <key>MINI</key> 
      <string>roset-ouv.png</string> 
      <key>CAT</key> 
      <string>MOTIFS</string> 
     </dict> 

、ここでは私のrootcontroller.mはあなたの非常に便利なの助けを

// RootViewController.m 
// FichesRaces 
// 
// Created by a3116b on 28/05/11. 
// Copyright 2011 __MyCompanyName__. All rights reserved. 
// 

#import "RootViewController.h" 
#import "FichesRacesAppDelegate.h" 
#import "CatsList.h" 
#import "DetailViewController.h" 
#import "NewsCustomCell.h" 
#import "InfoViewController.h" 

@implementation RootViewController 
@synthesize tabWebSites; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 





    // Charger le fichier .plist dans un tableau que l'on appelera arrayFromFile 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cats" ofType:@"plist"]; 
    NSDictionary *dictFromFile = [[NSDictionary alloc] initWithContentsOfFile:path]; 
    NSArray *arrayFromFile = [dictFromFile objectForKey:@"Root"]; 




    // Créons un tableau temporaire que nous allons remplir avec un objet Website par NSDictionnary contenu dans le fichier .plist 
    // Notez l'utilisation de NSEnumerator pour parcourir un tableau 
    NSMutableArray *websitesToAdd = [[NSMutableArray alloc] init]; 
    NSEnumerator *enumerator = [arrayFromFile objectEnumerator]; 
    NSDictionary *anObject; 
    while ((anObject = [enumerator nextObject])) { 
     CatsList *cl = [[CatsList alloc] initWithDictionaryFromPlist: anObject]; 
     [websitesToAdd addObject: cl]; 

     [cl release]; 
    } 

    // Remplir la propriété tabWebSites avec le contenu du NSMutableArray précédent 
    self.tabWebSites = [NSArray arrayWithArray:websitesToAdd]; 

    // Gestion de la mémoire : pour chaque alloc, n'oubliez pas le release qui va avec ! 
    [websitesToAdd release]; 
    [arrayFromFile release]; 
} 




- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 


} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 



// définir hauteur cellule 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath { 

    return 80; 
} 



// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // On n'a besoin que d'une section pour nos sites Internet 
    return 2; 
} 



//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
// NSArray *sectionTitles = [[NSArray alloc] 
    //       initWithObjects:@"Les Motifs", @"Les Couleurs", nil]; 
    // return sectionTitles; 
//} 




- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section 
{ 


    if (section == 0) return @"Les Motifs"; 
    if (section == 1) return @"Les Couleurs"; 
    return @"Other"; 
} 



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Nous ne tenons pas compte du numéro de section puisqu'il n'y en a qu'une 
    // Dans cette unique section il y a tous les éléments du tableau, on retourne donc le nombre 
    // return [self.tabWebSites count]; 


    if (section == 0) return 5; 
    if (section == 1) return 14; 
     return 0; 

} 

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

    static NSString *CellIdentifier = @"NewsCustomCellIdentifier"; 
    NewsCustomCell *cell = (NewsCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell==nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NewsCustomCell" owner:self options:nil]; 
     for (id oneObject in nib) { 
      if ([oneObject isKindOfClass:[NewsCustomCell class]]) 
       cell = (NewsCustomCell *)oneObject; 
     } 
    }  



    // determine the correct row. 
    // it will be restarted from 0 every time, and as 
    // we're just loading in from one array, we need to 
    // offset it by the right amount depending on the section. 
    int theRow = indexPath.row; 
    if (indexPath.section == 1) theRow += 6; 
    if (indexPath.section == 2) theRow += 19; 



    // On récupère l'objet Website qui correspon à la ligne que l'on souhaite afficher 
    CatsList *cl = [self.tabWebSites objectAtIndex:indexPath.row]; 

    // On configure la cellule avec le titre du site et sa description 
    cell.textLabel.text = cl.TITLE; 
    cell.detailTextLabel.text = cl.DESCRIPTION; 

    UIImage *img = [UIImage imageNamed:cl.MINI];  
    cell.imageView.image = img; 

    //important ajouter signalisation sinon APP REFUSE 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 



    // On renvoie la cellule configurée pour l'affichage 
    return cell; 
} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    DetailViewController *detailVC = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; 
    detailVC.CL = [self.tabWebSites objectAtIndex:indexPath.row]; 

    [self.navigationController pushViewController:detailVC animated:YES]; 
    [detailVC release]; 



} 
- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 
} 

- (void)dealloc 
{ 
     [super dealloc]; 
} 

@end 

おかげ

+0

super ces commentaires enfrançais! –

答えて

0

は、テーブルビューのデータソースが情報を提供するように設計されていますテーブルビューに移動します。値を表示する必要があるときは、そのデータソース、セクションの数、特定のセクションの行の数を尋ねます。セクションの数を返すメソッドは必須ではなく、デフォルトでは1が返されます。しかし、これは常に実装するための良い方法です。 あなたのコードのコメントから理解できるのは、セクションは1つだけです。

// not required 
-(NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    // you said only one section 
    return 1; 
} 

だから、今あなたがセクションの正しい数を返す、テーブルビューは、セクション(だけでも一つのセクションで)あたりにある行数を知る必要があり、これはで行わ:ある、 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; が必要ですメソッド。

-(NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    // return here the number of rows 
    // you have only one, don't check the section number... 
    return [self.tabWebSites count]; 
} 

最後に、データソースはすべてのセルをテーブルビューに戻す必要があります。私は-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;で試してみると、sectionを整数と比較しています。 sectionNSIndexPathのタイプで、intまたはNSUIntegerではないため、このように比較することはできません。代わりに、あなたがそれぞれのセクションと行番号を取得することができます。

NSUInteger sectionNumber = [indexPath section]; 
NSUInteger rowNumber = [indexPath row]; 

そして、これら二つの数字で、あなたは配列内の正しいオブジェクトを取得し、セルの内容を設定することができます。

有用な情報はAppleのTable View Programming Guide for iOSです。お役に立てれば。

+0

助けてくれてありがとう、 どのように配列の正しいオブジェクトを取得するには? セクション番号が1つしかないので、2つの数字 – a3116b

+0

を使用する方法がわかりません。データを含む1つの配列があり、 'id myDataObject = [myArray objectAtIndex:[indexPath row]];'の値を選択するだけです。私の答えがあなたのニーズに合っている場合は、左のチェックマークをクリックすることを忘れないでください;)([FAQ](http://stackoverflow.com/faq)) –

関連する問題