[tableView reloadData]; doesn't reload until navigating back to root controller - ios

A friend and I are working on a grocery list app and having some trouble with [tableView reloadData];. We're storing our grocery lists in plists and have an add items subview. When you leave that subview to return to the list, the list doesn't contain the new items even though we are updating the plist and calling reloadData in viewWillAppear. It does work though; if we navigate back to the master list of all lists and then back into the specific list we just added items to, the items then show up. Similarly, we want to delete items from the list by just selecting the row and having a checkmark appear next to the item. The list doesn't automatically update when a row is selected though, even though we're calling reloadData in didSelectRowAtIndexPath as well. Again, the change shows up if we navigate back to the master list and then go back into the individual list. I've checked and the delegate and dataSource in the nib files are correctly linked up to File's Owner.
Here's the code for the viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// THIS IS WHERE YOU PASS THE VARIABLE
iGrocerAppDelegate *passTest = [[UIApplication sharedApplication] delegate];
NSString *listName = [passTest passList];
[passTest setPassList:listName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullListName = [NSString stringWithFormat:#"%#.plist", listName];
NSString *file = [[paths lastObject] stringByAppendingPathComponent:fullListName];
self.list = [[NSArray alloc] initWithContentsOfFile:file];
[listTableView reloadData];
}
and didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:
indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
int row = [indexPath row];
NSString *deleteItem = [self.list objectAtIndex:row];
[self.itemsToDelete addObject:deleteItem];
// RECEIVE PASSED VARIABLE
iGrocerAppDelegate *test = (iGrocerAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *listName = [test passList];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullListName = [NSString stringWithFormat:#"%#.plist", listName];
NSString *file = [[paths lastObject] stringByAppendingPathComponent:fullListName];
//NSMutableArray *groceryLists = [[NSMutableArray alloc] initWithContentsOfFile:file];
//NSMutableArray *groceryList1 = [[NSMutableArray alloc] initWithContentsOfFile:file];
NSMutableArray *groceryList = [[NSMutableArray alloc] initWithContentsOfFile:file];
NSMutableArray *removeFromList = self.itemsToDelete;
int itemCount = [removeFromList count];
NSLog(#"Count: %d", itemCount);
for(int i=0; i < itemCount; i++) {
NSString *item = [removeFromList objectAtIndex:i];
NSLog(#"%#", item);
[groceryList removeObject:item];
NSLog(#"Middle ARRAY: %#", groceryList);
//[groceryList addObjectsFromArray: addToList];
}
NSLog(#"FINAL ARRAY: %#", groceryList);
if(![groceryList writeToFile:file atomically:YES]) {
NSLog(#"unsuccessful");
} else {
NSLog(#"successful");
}
NSLog(#"file name: %#",file);
self.list = [[NSArray alloc] initWithContentsOfFile:file];
NSLog(#"updated grocery list: %#", self.list);
[listTableView beginUpdates];
//[listTableView reloadData];
[listTableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
[listTableView endUpdates];
printf("list table view reloaded");
}
Any help would be much appreciated! Thanks in advance!

Solved.
It just needed to be:
[self.tableView reloadData];

Related

Objective-c I need to get a string of an array from a UItableview

I have a tableview that the rows are been filled with the name of .csv files that are in the document directory, i need to open the .csv that is been selected in the row and then show the info in a detail view. I'm trying to get the indexPath.row that it's been selected to get the correct element in the array but is crashing here's is that part of the code and above i will post the full code:
-(UITableViewCell *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%d", indexPath.row);
currentcsvfile = [dirList objectAtIndex:indexPath.row ];;
NSLog(#"\n current csv %#",currentcsvfile);
[self performSegueWithIdentifier:#"detailsegue" sender:self];
}
This is the error:
2017-10-09 22:51:31.590248 oFiOSstoryboard[2340:583894] 2
2017-10-09 22:51:31.590460 oFiOSstoryboard[2340:583894] *** -[__NSArrayI objectAtIndex:]: message sent to deallocated instance 0x170246540
![error
]1
Here is the full code:
#import "CameraViewController.h"
#import "resultsDetailView.h"
#interface CameraViewController ()
#property (strong, nonatomic) IBOutlet UITableView *data;
#property (retain, nonatomic) IBOutlet UILabel *timeStamp;
#end
////////////////////////csv readder
NSMutableArray *tableDataArray;
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *manager = [NSFileManager defaultManager];
NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot];
NSString *filename;
NSMutableArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *strPath = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"csv"];
NSString *strFile = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];
NSMutableArray *timeStampb = [[NSMutableArray alloc] init]; ;
NSMutableArray *arrayToDelete = [[NSMutableArray alloc] init]; ;
NSMutableArray *filePathsArray ;
NSMutableArray *dirList= [[NSMutableArray alloc] init]; ;
NSString *currentcsvfile;
#implementation CameraViewController
#synthesize data;
- (void)viewDidLoad {
[super viewDidLoad];
// ////lista de documentos
self.data.scrollEnabled = YES;
self.data.delegate = self;
self.data.dataSource = self;
filePathsArray =[[NSMutableArray alloc] init]; ;
NSMutableArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSMutableArray* fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
//--- Listing file by name sort
NSLog(#"\n File list %#",fileList);
//---- Sorting files by extension
NSMutableArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF EndsWith '.csv'"];
filePathsArray = [filePathsArray filteredArrayUsingPredicate:predicate];
NSLog(#"\n\n Sorted files by extension %#",filePathsArray);
dirList = filePathsArray;
NSString *docPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
if (!strFile) {
NSLog(#"Error reading file.");
}
[timeStampb release];
timeStampb = [[NSMutableArray alloc] initWithArray:[strFile componentsSeparatedByString:#"\,"]];
// this .csv file is seperated with new line character
// if .csv is seperated by comma use "," instesd of "\n"
for(NSString *countryname in timeStampb) {
NSLog(#"%#", timeStampb);
}
}
////////////////////////////////////////////////////////////////Delete csv files
//- (IBAction)delet:(id)sender {
// NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//
// NSString *filePath = [docPath stringByAppendingPathComponent:#"jorge.csv"];
// NSError *error = nil;
// [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
# pragma – mark table view DataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dirList count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier ];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
UIColor* color = [UIColor colorWithRed:(254.0/255.0) green:(251.0/255.0) blue:(248.0/255.0) alpha:1];
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:(253.0/255.0) green:(0.0/255.0) blue:(237.0/255.0) alpha:1];
[cell setSelectedBackgroundView:bgColorView];
cell.backgroundColor = color;
}
cell.textLabel.text = [timeStampb objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [dirList objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor colorWithRed:(0.0/255.0) green:(0.0/255.0) blue:(0.0/255.0) alpha:1];
cell.textLabel.font=[UIFont systemFontOfSize:8.0];
cell.detailTextLabel.font=[UIFont systemFontOfSize:15.0];
cell.detailTextLabel.textColor = [UIColor colorWithRed:(235.0/255.0) green:(120.0/255.0) blue:(33.0/255.0) alpha:1];
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%d", indexPath.row); // you can see selected row number in your console;
currentcsvfile = [dirList objectAtIndex:indexPath.row ];;
NSLog(#"\n current csv %#",currentcsvfile);
[self performSegueWithIdentifier:#"detailsegue" sender:self];
}
- (IBAction)deleteRow:(id)sender {
//we are not in edit mode yet
if([self.data isEditing] == NO){
//up the button so that the user knows to click it when they
//are done
[self.data setTitle:#"Done"];
//set the table to editing mode
[self.data setEditing:YES animated:YES];
}else{
//we are currently in editing mode
//change the button text back to Edit
//take the table out of edit mode
[self.data setEditing:NO animated:YES];
}
}
#pragma mark – TableView delegate
- (void)dealloc {
[_timeStamp release];
[dirList release];
self.data.delegate = nil;
self.data.dataSource = nil;
[super dealloc];
}
#end
When you get the array back from the file manager, retain it.
Also, there's no need for filePathsArray =[[NSMutableArray alloc] init]; ; since you are about to overwrite it with the returned results.
At line
dirList = filePathsArray;
You put autorelease object into variable ‘dirList’. Also you have a memory leak here.
If you need to put any objects from one array to other, use method addObjectsFromArray:. If you need to put other array into variable, use release/retain routine (Memory management in Objective-C)

Read/write data from a plist of dictionaries and arrays, and load different levels into a TableView

I'm a little confused about working with property lists. I've read most of the other questions about this topic, but I'm still very confused since they only go in one layer, so any help would be appreciated.
I would like to load a plist that stores data somewhat like this:
I have three ViewControllers (two TableViewControllers and one blank) in my Storyboard that I want to display different levels of information:
Categories of people (Friends, etc.)
Names of the people in those categories (Bob, etc.)
Information about the people (favorite color, etc.)
How do I read/write files to a plist, and how to I access that information across multiple views?
Once I read the data, how do I display the information in different TableViewControllers?
Let's say that you have a plist file like this:
and this code:
#implementation ViewController
NSDictionary *dictionary;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *array=[[NSMutableArray alloc]init];
array=[NSMutableArray arrayWithContentsOfFile:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:#"root.plist"]];
dictionary = [array objectAtIndex:0];
}
- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"numberOfSectionsInTableView:%lu",(unsigned long)dictionary.count);
return dictionary.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[[dictionary allKeys] objectAtIndex:section] capitalizedString];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSArray *peopleArray = [dictionary objectForKey:[[dictionary allKeys] objectAtIndex:indexPath.section]];
cell.textLabel.text = [[peopleArray objectAtIndex:indexPath.row]objectForKey:#"name"];
cell.detailTextLabel.text = [NSString stringWithFormat:#"Color:%# - Gender:%#",[[peopleArray objectAtIndex:indexPath.row]objectForKey:#"favorite color"],[[peopleArray objectAtIndex:indexPath.row]objectForKey:#"gender"]];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *key =[[dictionary allKeys] objectAtIndex:section];
NSLog(#"numberOfRowsInSection:%lu",(unsigned long)[[dictionary objectForKey:key] count]);
return [[dictionary objectForKey:key] count];
}
it will give you this output:
(Supposed you have a tableView set up with Delegate and DataSource)
I know that you asked for having the "Friends" and "Enemies" in different TableViews, but I use both in the same tableView but in different sections. But my code can get you started.
If you need extra help about the UITableView, see Apple UITableView Class Reference
If this is necessary, I used this code to generate the example plist-file in my test-project:
NSMutableArray *root=[[NSMutableArray alloc]init];
NSMutableArray *friendsarray = [[NSMutableArray alloc]init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:#"Jill" forKey:#"name"];
[dict setObject:#"green" forKey:#"favorite color"];
[dict setObject:#"female" forKey:#"gender"];
[friendsarray addObject:dict];
NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
[dict2 setObject:#"Bob" forKey:#"name"];
[dict2 setObject:#"Blue" forKey:#"favorite color"];
[dict2 setObject:#"male" forKey:#"gender"];
[friendsarray addObject:dict2];
NSMutableArray *enemiesarray = [[NSMutableArray alloc]init];
NSMutableDictionary *dict3 = [[NSMutableDictionary alloc]init];
[dict3 setObject:#"Michael" forKey:#"name"];
[dict3 setObject:#"Red" forKey:#"favorite color"];
[dict3 setObject:#"male" forKey:#"gender"];
[enemiesarray addObject:dict3];
NSMutableDictionary *d = [[NSMutableDictionary alloc]init];
[d setObject:friendsarray forKey:#"friends"];
[d setObject:enemiesarray forKey:#"enemies"];
[root addObject:d];
[root writeToFile:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:#"root.plist"] atomically:YES];
There are numerous examples here in Stack Overflow of reading plist and passing data between ViewControllers. Its advisable to read through them & ideally connect them together to get your solution.
Hope that helps.
You have some example like this : http://iosdevelopertips.com/data-file-management/reading-a-plist-into-an-nsarray.html
it's pretty easy.

Using a UITable to link to different pages in a notebook app (iOS)

I'm making a note taking app for the iPad, and it lets the user draw lines, and right now, it can save the pages of the notebook by saving each page as a PNG in the documents directory. Here's the code I have to save the images:
- (IBAction)saveImage:(id)sender {
UIImage *saveImage = drawImage.image;
if (saveImage != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"%#-%d.png", #"image", numberOfPages] ];
NSData* data = UIImagePNGRepresentation(saveImage);
[data writeToFile:path atomically:YES];
}
}
Just as a side note, numberOfPages is an int that is set to add 1 each time a "new page" button is pressed, this way, each image is named "image1", "image2", etc.
So my question is: How would I set up a UITable so the user can see a list of all the pages they've made.
Thanks so much for your time,
Karl
Each time you make a new page, add a string with the image's name to an array. Then, iterate through the array to populate a UITableView. When the user selects a cell, open the file with that name.
#import "RootViewController.h"
#implementation RootViewController
#synthesize keys, names;
AppDelegate *appD;
- (void)viewDidLoad
{
[super viewDidLoad];
appD = (AppDelegate *)[[UIApplication sharedApplication] delegate];
names = appD.data;
NSArray *array = [[names allKeys] sortedArrayUsingSelector:#selector(compare:)];
keys = array;
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [keys count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
return [nameSection count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = indexPath.section;
NSUInteger row = indexPath.row;
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [appD.data objectForKey:key];
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section];
key = [key substringFromIndex:2];
return key;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = indexPath.section;
NSUInteger row = indexPath.row;
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [appD.data objectForKey:key];
appD.theInfo.primary = [nameSection objectAtIndex:row];
[self performSegueWithIdentifier:#"secondarySegue" sender:self];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
This is for a table in a navigation controller that populates itself from a .plist and moves to another table when a cell is clicked. The tableview is connected to the view controller with delegate and datasource outlets.

Load Plist in UITableView for only 10 row

Actually this is to follow up my question before
UITableView show only first row.
Now my problem is that I only want to view the 10 list in my plist. If there are 11 items, the first item will be replace by the second and so on so my list only 10 items.
And this is my code to save to the plist:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Data.plist"];
NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (nil == array) {
array = [[NSMutableArray alloc] init];
}
NSMutableArray *list = [[NSMutableArray alloc]init];
[list addObject:resi.text];
[array addObject:list];
[array writeToFile:plistPath atomically: TRUE];
And this is the whole code for my table view that have been modified
- (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];
}
NSArray *list = (NSArray *)[self.array objectAtIndex:indexPath.row];
if(list && [list count] > 0) { //to check and avoid any crash
cell.textLabel.text = [list objectAtIndex:0];
}
// Configure the cell...
return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Data.plist"];
array = [NSMutableArray arrayWithContentsOfFile:plistPath];
[myHistoryTable reloadData];
}
if ([array count] > 10) {
array = [array subarrayWithRange:NSMakeRange([array count] - 10, 10)];
}
if you don't want to overwrite the original array create a second one which serves as the dataSource of the tableView:
array = /* load from plist */;
if ([array count] > 10) {
self.dataSourceArray = [array subarrayWithRange:NSMakeRange([array count] - 10, 10)];
}
else {
self.dataSourceArray = array;
}

Error reading plist file for fill a table

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;
}
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.
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.

Resources