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!