2011-07-19 20 views
0

我是初學者在Xcode編碼。 我想要得到的是從plist的sectionview中加載tableview。 而我的問題是在我的部分正確放置行。Xcode:問題與表中的部分

在我的plist中,我在每個條目中都有我的部分名稱,但我不知道如何獲取這些信息。所以我通過代碼添加了我的兩部分的名字。

我的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! – 2011-07-19 06:18:07

回答

0

表視圖的數據源的目的是提供信息到表格視圖。當它需要顯示這些值時,它會詢問它的數據源,那裏有多少部分,特定部分有多少行。返回部分數量的方法不是必需的,默認情況下會返回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的類型,不是intNSUInteger,所以你不能這樣比較。相反,你可以檢索部分和行號,分別:

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

然後用這兩個數字,你可以檢索數組中的正確的對象,並設置單元格的內容。

您可以在Apple的Table View Programming Guide for iOS找到有用的信息。希望這可以幫助。

+0

感謝您的幫助, 但如何檢索數組中的正確對象? 我不知道如何使用這兩個數字 – a3116b

+0

,因爲你只有一個部分,你可能有一個包含數據的數組,只需在'id myDataObject = [myArray objectAtIndex:[indexPath row]];'中選取值。此外,如果我的答案符合您的需求,請不要忘記單擊左側的複選標記;)([FAQ](http://stackoverflow.com/faq)) – 2011-07-19 07:13:44