简体   繁体   English

在我剖分的表格视图的右行中显示好数据(来自plist)的问题

[英]Problem to display the good data (from a plist) in the right row in my sectioned tableview

I've a sectionned tableview with 3 sections (Motif - Couleur - Other) The data of the tableview are loaded from a plist witch looks like this 我有一个包含3个部分(Motif-Couleur-Other)的分段tableview,该tableview的数据是从plist女巫加载的,看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<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>La robe du bengal dit &quot;Brown&quot; est d&apos;une infinie richesse. Loin de se limiter ou brun, elle passe également par des tons de roux vifs de blonds dorés . Les yeux sont jaunes dorés ou verts. Bien connu du public, les bengals bruns sont toujours aussi populaires et recherchés.</string>
            <key>TITLE</key>
            <string>Brown</string>
            <key>IMAGE</key>
            <string></string>
            <key>MINI</key>
            <string></string>
            <key>CAT</key>
            <string>COULEURS</string>
        </dict>

the name of the section is store in my plist, but at the moment i don't know how to pick the name (CAT MOTIFS) in this plist and display it in the header of my sectionned tableview. 该部分的名称存储在我的plist中,但此刻我不知道如何在该plist中选择名称(CAT MOTIFS)并将其显示在我分段的tableview的标题中。 then a code it differently and it seens to work but the name of sections don't come from the plist. 然后以不同的方式进行编码,它看起来可以工作,但是节的名称并非来自plist。

that one of the problem that made me crasy 那让我疯狂的问题之一

and the second one, the most important, in the first section i want to display 4 rows, 9 in the second section, and 3 in the third section. 第二个是最重要的,在第一部分中,我要显示4行,第二部分中显示9行,第三部分中显示3行。 the problem is that the app display a good sectioned tableview, with 3 sections well named, with the good number of row in each sections, but not the good data in the section actualy the row one contain data 1-2-3-4, but the second row contain the data 1-2-3-4-5.... and what i what is to display the data 5-6-7.... 问题在于该应用显示了一个良好的分区表视图,其中3个分区的名称很好,每个分区中的行数都很好,但是该分区中的数据不好,实际上第一行包含了数据1-2-3-4,但是第二行包含数据1-2-3-4-5 ....以及显示数据5-6-7 ....的内容

i hope you understand what my problem is. 我希望你了解我的问题。 Here is my rootviewcontroller.m 这是我的rootviewcontroller.m

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

#import "RootViewController.h"
#import "CatsList.h"
#import "DetailViewController.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];
}



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

    if ( section == 0 ) return @"Les Motifs";
    if ( section == 1 ) return @"Les Couleurs";
    if ( section == 2 ) return @"Bon à Savoir";

    return @"";

   // return [sectionHeaders objectAtIndex:section];
}






// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 4;
            break;

        case 1:
            return 13;
            break;

        default:
            return 3;
            break;
    }

   }







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

    static NSString *CellIdentifier = @"Cell";
    if (indexPath.section == 2) {
        CellIdentifier = @"cell";
    }
//-----------------------------------------------

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

    // 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 sectionNumber = indexPath.row;
    if ( sectionNumber == 0 ) rowNumber = 4;
    if ( sectionNumber == 1 ) rowNumber = +5;
    if ( sectionNumber == 2 ) rowNumber = +18;


//-----------------------------------------------    

    // Les cellules sont mises en cache pour accélérer le traitement, sous l'identifiant "Cell",
    // on essaie récupère ce modèle de cellule s'il est déjà en cache
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Si on n'a pas réussi à sortir une cellule du cache, on crée un nouveau modèle de cellule
    // et on l'enregistre dans le cache
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }





    // 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;


    //important ajouter signalisation sinon APP REFUSE

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

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



- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 3) {
        return nil;
    }
    return indexPath;
}





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

thanks for your very useful help 感谢您的有用帮助

It looks like you're using a rowNumber variable to store an offset row index in tableView:cellForRowAtIndexPath: but then you aren't doing anything with that amended value. 似乎您正在使用rowNumber变量将偏移行索引存储在tableView:cellForRowAtIndexPath:但是,您并没有使用该修改后的值做任何事情。

Edit 编辑

From what I can see maybe you should try this: 从我所看到的也许您应该尝试此:

CatsList *cl = [self.tabWebSites objectAtIndex:rowNumber];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM