Replaceing a string on a plist - ios

I am making some confing settings using a tableView using a plist File to fill the data I am working around this code about 2 weeks and this appears to work.
This is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([[[week objectAtIndex:indexPath.row] objectForKey:#"checkmark"] isEqual:#"0"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[[week objectAtIndex:indexPath.row] setObject:#"1" forKey:#"checkmark"];
}
else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[[week objectAtIndex:indexPath.row] setObject:#"0" forKey:#"checkmark"];
}
[[week objectAtIndex:indexPath.row] writeToFile:#"week.plist" atomically:YES];
NSLog(#"%#, %#", selectedCell.textLabel.text,[[week objectAtIndex:indexPath.row] objectForKey:#"checkmark"]);
NSLog(#"Array de week seleccionando %#", week);
}
Everything seems to work fine on the debbug area (that's why I use the nslogs), and my problem is That can't save the changes on the plist file, it seem it saving in some kind of cache or something like that.
Update
I've also tried with this code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *plistPath = [rootPath stringByAppendingPathComponent:#"week.plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:week forKeys:week];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:nil];
if ([[[week objectAtIndex:indexPath.row] objectForKey:#"checkmark"] isEqual:#"0"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
NSString *uno = #"1";
[[week objectAtIndex:indexPath.row] setObject:uno forKey:#"checkmark"];
}
else if ([[[week objectAtIndex:indexPath.row] objectForKey:#"checkmark"] isEqual:#"1"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
NSString *cero = #"0";
[plistData writeToFile:plistPath atomically:YES];
}
[[week objectAtIndex:indexPath.row] setObject:cero forKey:#"checkmarck"];
NSLog(#"%#, %#", selectedCell.textLabel.text,[[week objectAtIndex:indexPath.row] objectForKey:#"checkmark"]);
NSLog(#"Array de checkmarks %#", week);
}
And I have had the same result on the Debug Area:
2014-07-07 22:11:21.384 experiment table view[2264:60b] Thursday, 0
2014-07-07 22:11:21.385 experiment table view[2264:60b] Array de week seleccionando (
{
Name = Sunday;
checkmarck = 0;
},
{
Name = Monday;
checkmarck = 1;
},
{
Name = Tuesday;
checkmarck = 0;
},
{
Name = Wednesday;
checkmarck = 0;
},
{
Name = Thursday;
checkmarck = 0;
},
{
Name = Friday;
checkmarck = 1;
},
{
Name = Saturday;
checkmarck = 0;
}
)
Which is the same result that I get

If the Plist is in the bundle, you cannot write into it. 'In the bundle' means that you create the Plist by adding a new file in XCODE manually.
You can only write into a Plist that you create programmatically. Such a Plist is not in the bundle, but in your app's document directory. The path of the Plist is
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = #"week.plist";
NSString *path = [NSString stringWithFormat:#"%#/%#", documentsDirectory, fileName];

Thanks to Sen I resolve this, his answer lead me to correct my code.
First I had to programmatically make the pList:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"week.plist"];
NSMutableArray *week = [[NSMutableArray alloc]init];
//Here I add all the objects inside the Mutable Array
[week writeToFile:path atomically:YES];
Then to write on the Plist by selecting it on the picker:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"week.plist"];
NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:path];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([[[array objectAtIndex:indexPath.row] objectForKey:#"checkmark"] isEqual:#"0"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[[array objectAtIndex:indexPath.row] setObject:#"1" forKey:#"checkmark"];
} else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[[array objectAtIndex:indexPath.row] setObject:#"0" forKey:#"checkmark"];
}
[array writeToFile:path atomically:YES];
[tableView reloadData];
[self.navigationController pushViewController:self animated:YES];
}
Thanks, and I hope this code help someone.

Related

display data from plist to tableCell

i have a pilist whose screen shot is given below:
i want to display its data in table cell. my code is
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSString *path = [[NSBundle mainBundle] pathForResource:#"datalist" ofType:#"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
tableData = [dict objectForKey:#"title"];
thumbnails = [dict objectForKey:#"Subtitle"];
cell.textLabel.text = [NSString stringWithFormat:#"%#",thumbnails];
return cell;
}
I am getting this output:
Your code thumbnails = [dict objectForKey:#"Subtitle"]; will return an NSArray.
What you should be doing is iterating through this array and getting each item.
Like cell.textLabel.text = [NSString stringWithFormat:#"%#",[thumbnails objectAtIndex:0]] or cell.textLabel.text = [NSString stringWithFormat:#"%#",[thumbnails objectAtIndex:1]]
Or maybe something like this
cell.textLabel.text = [NSString stringWithFormat:#"%# %#",[thumbnails objectAtIndex:0], [thumbnails objectAtIndex:0]]
Try it.
Try This One:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"datalist.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:#"datalist" ofType:#"plist"];
}
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
tableData = [dict objectForKey:#"title"];
thumbnails = [dict objectForKey:#"Subtitle"];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[thumbnails objectAtIndex:indexPath.row]];
return cell;
thumbnails is an Array. So When you retrieve the data from Array, Use [thumbnails objectAtIndex:indexPath.row] .

in UITableView first entry and last entry switching record data when loading from server

I am developing an app that will display data from server in a parallax type UITableView, and here is my code. Everything is loading great, but cell data(image, etc) keep switching from one cell to another.
- (void)viewDidLoad
{
[self hasInternet];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self loadData];
self.edgesForExtendedLayout=UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets=NO;
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[self scrollViewDidScroll:nil];
[super viewWillAppear:animated];
[self loadData];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
- (void) loadData{
name = #"name";
email = #"email";
thumbnail = #"thumbnail";
myObject = [[NSMutableArray alloc] init];
NSData *jsonSource = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://URL.php"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects) {
NSString *title_data = [dataDict objectForKey:#"fname"];
NSString *title_data2 = [dataDict objectForKey:#"lname"];
NSString *fulname = [NSString stringWithFormat:#"%# %#", title_data, title_data2];
NSString *emAil = [dataDict objectForKey:#"email"];
NSString *thumbnail_data = [dataDict objectForKey:#"img"];
thumbnail_data = [NSString stringWithFormat:#"http://URL/upload/%#",thumbnail_data];
dictionary = [NSDictionary dictionaryWithObjectsAndKeys: fulname, name, emAil, email, thumbnail_data, thumbnail, nil];
[myObject addObject:dictionary];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return myObject.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"parallaxCell";
JBParallaxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell=[[JBParallaxCell alloc]initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
NSMutableString *text;
text = [NSMutableString stringWithFormat:#"%#",[tmpDict objectForKeyedSubscript:name]];
NSMutableString *mail;
mail = [NSMutableString stringWithFormat:#"%#",[tmpDict objectForKeyedSubscript:email]];
NSMutableString *images;
images = [NSMutableString stringWithFormat:#"%# ",[tmpDict objectForKey:thumbnail]];
NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
NSData *data = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
cell.parallaxImage.image = [[UIImage alloc]initWithData:data];
});
});
cell.titleLabel.text = [NSString stringWithFormat:#"%#",text];
cell.subtitleLabel.text = [NSString stringWithFormat:#"%#",mail];
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSArray *visibleCells = [self.tableView visibleCells];
for (JBParallaxCell *cell in visibleCells) {
[cell cellOnTableView:self.tableView didScrollOnView:self.view];
}
}
When I compile it, it shows all my data but then keep switching from one cell to another. Any help will be appreciated. Thanks
Because UITableView reuse the cell so when you scroll down or up the previous cell which is gone from your tableview bound is reuse. but it's image view has an image when it use last time so you have to clear first the imageview.
so put this line
cell.parallaxImage.image = nil;
Before the dispatch queue
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You can use this code it will save the images in document directry and then show it.
please change the variable name.
NSString *imageURL = [[matcheListArr objectAtIndex:indexPath.row] valueForKey:#"thumbnail_image"];
NSURL *url = [NSURL URLWithString:imageURL];
NSArray *seperate = [[[matcheListArr objectAtIndex:indexPath.row] valueForKey:#"thumbnail_image"] componentsSeparatedByString:#"/"];
NSString *fileName = [NSString stringWithFormat:#"%#.png",[seperate objectAtIndex:seperate.count-1]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/MyFolder"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *getImagePath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",fileName]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
if ([img isKindOfClass:[UIImage class]]) {
//Set Downloaded Image
[cell.matchUserImageView setImage:img];
}
else {
if ([[ValidationString sharedManager] isNullString:[[matcheListArr objectAtIndex:indexPath.row] valueForKey:#"thumbnail_image"]] == YES) {
//Set Default Image
[cell.matchUserImageView setImage:[UIImage imageNamed:#"photo_icon.png"]];
}
else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:url];
NSArray *seperate = [[[matcheListArr objectAtIndex:indexPath.row] valueForKey:#"thumbnail_image"] componentsSeparatedByString:#"/"];
NSString *fileName = [NSString stringWithFormat:#"%#.png",[seperate objectAtIndex:seperate.count-1]];
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
[cell.matchUserImageView setImage:[UIImage imageWithData:imageData]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/MyFolder"];
NSString *savedImagePath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",fileName]];
[imageData writeToFile:savedImagePath atomically:NO];
});
});
}
}

View plist data in UITableView

The thing is. i want to display the each object to the tableview not it's under subs so in this case. 123.
i can't seem to figured out how to display the that one item...
also. the way it saves the plist now it overwrites the plist it seems. since it's fresh created each time i go add
i followed numbers torturials on the web and here on stack overflow. but none seem to get close to what i want to do.
MainTableViewController.h
{
NSMutableDictionary *arrayD;
}
MainTableViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Property List.plist code
//Gets paths from root direcory.
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
//Get documents path.
NSString *documentsPath = [paths objectAtIndex:0];
//Get the path to our PList file.
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Servers.plist"];
//Check to see if Property List.plist exists in documents.
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
[[NSFileManager defaultManager]copyItemAtPath:[[NSBundle mainBundle]pathForResource:#"Servers" ofType:#"plist"] toPath:plistPath error:nil];
//If not in documents, get property list from main bundle.
// plistPath = [[NSBundle mainBundle] pathForResource:#"Servers" ofType:#"plist"];
}
//Read property list into memory as an NSData object.
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
//convert static property list into dictionary object.
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp){
NSLog(#"Error reading plist: %#, format: %d", errorDesc, format);
}
//Display values.
NSMutableDictionary *dictinoary =[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
tableData = [dictinoary objectForKey:#"Hostname"];
NSLog(#"Count: %i", [arrayD count]);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Hostname";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
AddServerViewController.m
- (IBAction)SaveData:(id)sender {
//Get paths from root direcory.
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
//Get documents path.
NSString *documentsPath = [paths objectAtIndex:0];
//Get the path to our PList file.
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"Servers.plist"];
//Set the variables to the values in the text fields.
hostName = hostEntered.text;
portNumber = portEntered.text;
userName = userNameEntered.text;
passWord = passWordEntered.text;
NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4];
NSMutableDictionary *data;
data = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects: hostName, portNumber, userName, passWord, nil] forKeys:[NSArray arrayWithObjects:#"Hostname", #"Port", #"Username", #"Password", nil]];
[rootObj setObject:data forKey:hostName];
NSString *error = nil;
//Create NSData from dictionary.
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
//Check is plistData exists.
if(plistData){
//Write plistData to our Properity List.plist file.
[plistData writeToFile:plistPath atomically:YES];
}
else{
NSLog(#"Error in saveData: %#", error);
}
}
Each dictionary has allKeys method that returns an array with... all keys.
If you do what Peter said and then you can just populate your tableView with allKeys
Forgive me if I misread your question but the following was done after recreating your plist:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Sample List" ofType:#"plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(#"Plist dict %#", [plistDict objectForKey:#"123"]);
That would output:
Plist dict {
Hostname = 123;
Password = 123;
Port = 123;
Username = 123;
}
Is that what you were looking for? If not, ill need a bit more detail with regards to what it is that you're looking for.

Cannot delete picture

I am saving my pictures into the collection view but I cannot delete the picture that I have took. I am using tap delete in the collection view. This is my code for that.However after I tap and delete the picture it looks like the picture is not deleted, and cannot find where is wrong, I am suspecting that it has something to do with the array but I am not sure.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
allImagesArray = [[NSMutableArray alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *locations = [[NSArray alloc]initWithObjects:#"Bottoms", #"Dress", #"Coats", #"Others", #"hats", #"Tops",nil ];
NSString *fPath = documentsDirectory;
NSMutableArray *trashCan = [NSMutableArray array];
NSArray *directoryContent;
for(NSString *component in locations){
NSString *TrashBin = [fPath stringByAppendingPathComponent:component];
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: TrashBin];
collectionTrash.delegate =self;
collectionTrash.dataSource=self;
for(NSString *str in directoryContent){
NSLog(#"str:%#", str);
NSString *finalFilePath = [TrashBin stringByAppendingPathComponent:str];
NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
[trashCan addObject:finalFilePath];
if(data)
{
UIImage *image = [UIImage imageWithData:data];
[allImagesArray addObject:image];
NSLog(#"array:%#",[allImagesArray description]);
}}}
Trash = trashCan;
for(NSString *folder in locations) {
for(NSString *file in directoryContent) {
// load the image
}
}}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
NSLog(#"j");
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [allImagesArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseID = #"ReuseID";
TrashCell *mycell = (TrashCell *) [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
UIImageView *imageInCell = (UIImageView*)[mycell viewWithTag:1];
imageInCell.image = [allImagesArray objectAtIndex:indexPath.row];
NSLog(#"a");
return mycell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"s:%d", [Trash count]);
NSString *trashBin = [Trash objectAtIndex:indexPath.row];
NSLog(#"k%#l",trashBin);
[allImagesArray removeObjectAtIndex:indexPath.row];
[self.collectionTrash reloadData];
[self deleteMyFiles:trashBin];
}
NSString *myFileName;
-(void) deleteMyFiles:(NSString*)filePath {
NSError *error;
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
}
Be sure you delete the image in local memory also. When you delete image from collectionView not delete. Then stop the app and run it again see the deleted image is removed or not?. I think this is your problem. Then store all the image in single array and store it in plist. When you remove any image replace the array after removing the image in the array.
Use
[collectionView reloadData]
after calling deleteMyFiles.

Image on UITableView Cell

i have different folder on document directory and have images on each folder. Now i want to display one image from different folder. i have tried but the program is crashe at the line.
- (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];
}
//I dont know what put here for display from sqlite
NSUInteger row = [indexPath row];
cell.textLabel.text = [array1 objectAtIndex:row];
//cell.detailTextLabel.text = [array2 objectAtIndex:row];
NSString *b = [array2 objectAtIndex:row];
NSLog(#"b=%#",b);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(#"%#",paths);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError *error = nil;
NSArray *imageFileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",b]] error:&error];
NSLog(#"file=%#",imageFileNames);
NSMutableArray *images = [[NSMutableArray alloc ] init];
for (int i = 0; i < [imageFileNames count]; i++)
{
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#/%d.png",b, i]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
[images addObject:img];
NSLog(#"%#",getImagePath);
}
NSLog(#"images=%#",images);
cell.imageView.image=[imageFileNames objectAtIndex:0];
//NSLog(#"row=%u",row);
return cell;
}
I think what you are looking at is something like this (please modify to your liking):
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
self.contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documents error:NULL];
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.fileLabel.text = [self.contents objectAtIndex:indexPath.row];
// Build Documents Path with Folder Name
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *directory = [documents stringByAppendingPathComponent:[self.contents objectAtIndex:indexPath.row]];
NSFileManager *fM = [NSFileManager defaultManager];
// Get Attributes
NSDictionary *fileAttributes = [fM attributesOfItemAtPath:directory error:NULL];
// Check if it's a directory
if ([fileAttributes objectForKey:NSFileType] == NSFileTypeDirectory) {
// Get contents of directory
NSArray *insideDirectory = [fM contentsOfDirectoryAtPath:directory error:NULL];
cell.folderImageView.image = nil;
// Loop through folder contents
for (NSString *file in insideDirectory) {
if ([[[file pathExtension] uppercaseString] isEqualToString:#"PNG"]) {
NSString *imagePath = [directory stringByAppendingPathComponent:file];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
cell.imageView.image = image;
break;
}
}
}
return cell;
}
You can directly use [UIImage imageNamed:#""] for images rather than imageWithContentsOfFile

Resources