2011-12-19 4 views
1

Je voudrais créer quelque chose comme la technologie "Ajouter aux favoris". Dans mon application, il y a un tabbar. Sur le premier onglet, il y a un formulaire avec un bouton. Et ce que je veux faire est quand vous appuyez sur le bouton, il enregistre le nom de la barre de navigation dans le tableau et enregistre tableau dans le plist. Et quand vous ouvrez le deuxième onglet, vous voyez une vue avec tableview dedans, et j'aimerais voir le nom de navigationBar ici, dans une cellule (ainsi un tableau prend l'information de plist et le met dans un tableView). Donc, voici mon code:Impossible de remplir TableView avec plist

// Info.h Its a class with button 
#import <UIKit/UIKit.h> 
@class Favorites; 
@interface Info : UIViewController 
{ 
    NSMutableArray *favGifts; 
    NSString *justAstring; 
} 
@property (nonatomic, retain) NSMutableArray *favGifts; 
@property (nonatomic, retain) NSString *justAstring; 

- (IBAction)save:(id)sender; 
-(NSString *)pathOfFile; 

@end 

//Info.m 

#import "Info.h" 

@implementation Info 

@synthesize ADFbutton; 
@synthesize justAstring; 
@synthesize favGifts; 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 
- (NSString *)pathOfFile { 

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentFolder = [path objectAtIndex:0]; 
    return [documentFolder stringByAppendingFormat:@"myFile.plist"]; 

} 
- (IBAction)save:(id)sende{ 

    NSString *m = self.navigationItem.title; 
    favGifts = [[NSMutableArray alloc]init]; 
    [favGifts addObject:m]; 
    [favGifts writeToFile:[self pathOfFile] atomically:YES]; 
    justAstring = self.pathOfFile; 
    NSLog(@"%@", justAstring); 
} 

//And this is class with the tableview inside: 

#import <UIKit/UIKit.h> 
@class Info; 
@interface Favorites : UIViewController 
<UITableViewDataSource>{ 

    UITableView *myTableView; 
    NSString *name; 
    Info *info; 
} 
@property (retain, nonatomic) IBOutlet UITableView *myTableView; 
@property (nonatomic,retain) NSString *name; 
@property (nonatomic, retain) Info *info; 

@end 
// .m 
#import "Favorites.h" 
#import "Info.h" 

NSMutableArray *array; 

@implementation NewFavGifts 

@synthesize myTableView; 
@synthesize giftInfo; 
@synthesize name; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

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

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

#pragma mark - View lifecycle 
- (void) viewWillAppear:(BOOL)animated{ 

    NSString *filePath = info.justAstring; 

    if ([[NSFileManager defaultManager]fileExistsAtPath:@"myFile.plist"]) { 
     array = [[NSMutableArray alloc]initWithContentsOfFile:filePath]; 

    } 

    [myTableView reloadData]; 
} 
- (void)viewDidLoad 
{ 


    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

- (void)viewDidUnload 
{ 
    [self setMyTableView:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    // Return the number of rows in the section. 
    return [array count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    NSString *cellValue = [array objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    return cell; 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 75; 
} 
@end 

Xcode ne montre aucune erreur, mais mon tableView est vide quand je l'ouvre. Je ne trouve pas mon erreur ou une solution pendant 4 jours.

Répondre

1

Vous avez à l'étape de tournage de la difficulté à pas

Vérifiez si vous obtenez quelque chose dans le tableau en ajoutant NSLog comme indiqué ci-dessous,

Si obtenir « Array Count 0 » dans la console, il ne veut rien dire est écrit dans le fichier plist

ou si vous n'êtes pas du tout trouvé le texte NSLog dans la console, cela signifie qu'il n'y a pas de fichier plist existe

- (void) viewWillAppear:(BOOL)animated{ 

    NSString *filePath = info.justAstring; 

    if ([[NSFileManager defaultManager]fileExistsAtPath:@"myFile.plist"]) { 
     array = [[NSMutableArray alloc]initWithContentsOfFile:filePath]; 

    NSLog(@"Array Count %d", [array count)); //add this line and check whether you get any thing in the array 
    } 

    [myTableView reloadData]; 
} 
+0

Merci pour anwser! La console montre 0. MAIS, tout en testant l'application sur le simulateur, je suis allé à la \ Bibliothèque \ Application support \ iphone simulateur \ 5.0 \ programm et découvre qu'il écrit la chaîne (nom de navBar) dans le plist chaque fois que j'appuie sur le bouton. – SmartTree

+0

Pouvez-vous poster le contenu du fichier plist – Nakkeeran

+0

Eh bien, je ne sais pas comment, mais il ressemble à ceci: Point clé 0 Type Valeur chaîne Globe – SmartTree

1

Il y a plusieurs choses que je changerais:

  • dans la définition d'interface, ajoutez UITableviewDelegate

    @interface Favoris: UIViewController < UITableViewDataSource, UITableViewDelegate> {

  • mettre votre NSArray *array dans l'interface , ajoutez une propriété nonatomic,retain

  • Remplissez le tableau avec self.array = [NSArray arrayWithContentsOfFile:...]. Il n'a pas besoin d'être NSMutableArray, mais utilisez self.array pour le conserver correctement.

+0

Merci! Je vais faire des corrections! – SmartTree

Questions connexes