views:

177

answers:

2

Hi, I'm developing an app for iPhone but I've a problem...
I've a view with some textField and the informations writed in them are saved in a plist file. With @class and #import declarations I import this view controller in another controller that manage a table view.
The code I've just wrote appear to be right but my table is filled up with 3 same row...
I don't know why the row are 3...

Can anyone help me?

This is the code for add info:

@implementation AddWishController

@synthesize titleField, linkField, descField;

-(NSString *)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:plistPath];
}

-(IBAction)done {
    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:titleField.text];
    [array addObject:linkField.text];
    [array addObject:descField.text];
    [array writeToFile:[self dataFilePath] atomically:YES];

    [array release];

    [self dismissModalViewControllerAnimated:YES];
}

And this control the table:

#import "WishlistController.h"
#import "AddWishController.h"

@implementation WishlistController

@synthesize lista;

-(IBAction)newWish {
    AddWishController *add = [[AddWishController alloc] initWithNibName:@"AddWish" bundle:nil];
    [self.navigationController presentModalViewController:add animated:YES];
    [add release];
}

-(NSString *)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:plistPath];
}

#pragma mark -
#pragma mark View lifecycle

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

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *data = [[NSArray alloc] initWithContentsOfFile:[self dataFilePath]];
    return [data count];
    [data release];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        cell.textLabel.text = [array objectAtIndex:0];
        cell.detailTextLabel.text = [array objectAtIndex:2];

        NSLog(@"%@", array);
    }

    return cell;
}

Thanks!

A: 

In WishController# load, you put three different items into the Array, So of course, if in WishlistController# tableView: numberOfRowsInSection: you return [data count] as the number of rows, you get three rows. On the other Hand, you in WishlistController# tableView: cellForRowAtIndexPath: you do not look, which entry should be presented, so it will always show only the one and only cell.

If you want to present only one cell in your table, you replace your WishlistController# tableView: numberOfRowsInSection: with

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

If you want to show multiple Wishes, you better make a real object of it, or at least use NSDictionary, where one entry in the Dictionary represents one object (=Wish) with multiple attributes (title, link, desc).



If for your first steps in Cocoa/Objective-C you really really want not to look into NSDictionary yet, you could also

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return floor([data count]/3);
}

and then in #tableView: cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        int row = indexPath.row * 3;

        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        cell.textLabel.text = [array objectAtIndex:row];
        cell.detailTextLabel.text = [array objectAtIndex:row+2];

        NSLog(@"%@", array);
    }

    return cell;
}

But this definitly is only for prototyping-to-get-something-displayed. You soon want to look into NSDictionary and before releasing anything, you better learn the use of proper objects for your Data Model, for instance with Core Data. Really.

two more things: -In WishlistController#tableView:numberOfRowsInSection: the line

[data release]; 

after the return never gets called. You probably want to

[data autorelease];

before the return-statement. - You do not want to read a file // look for your objects each time a row in your table gets displayed. Create an array as an instance-variable of your controller and use it as your datasource.

Mullzk
techzen of course is right: writeToFile replaces the content of the file, so there never is more than one wish in your File. So, the thing about slicing the array into items with 3 subitems is not only darn ugly, but utterly useless. Make yourself acquainted with proper mechanos for saving data (again: Core Data is your friend)
Mullzk
+1  A: 

Your first problem is that NSArray writeToFile: doesn't append to an existing file. It just writes the complete contents of the array to the file. If a file by the same name already exist, then the function overwrites it.

This means that in your code at present, you only ever save an array with three elements. No matter how many times the user saves in the AddWishController's view, only the last three pieces of information captured are ever preserved on disk.

Secondly, your WishlistController is misconfigured.

This:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *data = [[NSArray alloc] initWithContentsOfFile:[self dataFilePath]];
    return [data count];
    [data release];
}

Will always return a count of three because the array in the file always has three elements. However, even that is wrong because you don't want to display each element in separate cell. Your cellForRowAtIndexPath: very clearly puts the first and last elements of the array into each and every cell.

At the very least here, you need a two-dimensional array. Better yet you need an array of dictionaries. Each dictionary will hold the results of one "done" operation in -[AddWishController done]. Put add each dictionary to an array. Then in your tableview controller, return the count of the array for the number of rows in the table. Then in cellForRowAtIndexPath: you should get the dictionary in array element of indexpath.row. Then get the text for the cell UI elements from each entry in the dictionary.

Usually, you also would not save to file until the app quits. Instead, put the array in an property of the app delegate. In AddWishController create a property to refers to the app delegates property. Do the same in the tableview controller. Now you can populate the array in the first controller and read it from the second.

Basically, you need to start over from scratch.

TechZen
OK thanks for the help... Therefore I must create two NSDictionaries then I must add them in a NSArray. Right?
Matthew
You create a dictionary every time the user makes an entry in the AddWishController view that calls the `done` method. The point is to record each wish as a dictionary so that the array will be a list of wishes. In the table, each cell will display one wish so that the table is a list of wishes. The basic idea here is to have the data model reflect the table and have the table reflect the data model. That way, everything falls into place naturally.
TechZen