This question already has answers here:
Objective-C: how to add a new cell for each filename in UiTableView?
(2 answers)
Closed 9 years ago.
Bellow I have some code that retrieves files from the documents directory where my application is stored on the idevice. It then successfully retrieves that path to the file which is a mp4 because that is what I am storing. And does this by displaying the filename in a cell of the UiTableView I have created . However the code only displays one file in one cell. But I want to list multiple files individually in there own cell so eventually the user can individually select that cell depending on the video file they want.
Here is the code that retrieves and displays the file directory of the file but does not list more then one file in the table:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(!filePathsArray) // if data loading has been completed, return the number of rows ELSE return 1
{
if ([filePathsArray count] > 0)
return [filePathsArray count];
else
return 1;
}
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(!filePathsArray)
{
if ([filePathsArray count] > 0)
return [filePathsArray count];
else
return 1;
}
return 1;
}
Should read:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [filePathsArray count];
}
You should be returning [filePathsArray count]; from your numberOfRowsInSection method under normal circumstances. Right now you're actually returning 1 from that method in all cases. I'm not sure what exactly you're trying to do with the logic at the top of your numberOfRowsInSection method, but I think you should start by just replacing the whole body of the method with return [filePathsArray count];.
One more thing: You have a comment in there "if data loading has completed," but all the work in the code you posted is going synchronously on the main thread. By the time your viewDidLoad method has completed your filePathsArray is initialized. You don't need to do anything tricky to account for a case where filePathsArray is still nil, unless you're worried about the table view loading data before the view controller's view has actually loaded. But that would be very weird.
Why are you initializing filePathsArray over and over again first under viewDidLoad and then under tableview.
This is how I would write the code.
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//initializing array and getting values the first time when page loads.
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//return whatever the count of this array is. no need to do any if! checks
return [filePathsArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];
return cell;
}
Related
This question already has an answer here:
How To Reload Dictionaries On Tableview By Segment Button Click Using Objective C?
(1 answer)
Closed 7 years ago.
I am trying to create Plist dictionary data's read and load into tableview. Here, I am maintaining two segment control button and single tableview. Whenever I selected segment control button one I need to load first plist dictionary data then button two to load second one. I need to differentiate two data set and based on button click It should reload.
My Code:
// to read data
NSArray *pathse = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = pathse.firstObject;
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"port.plist"];
savedUrl = [[NSMutableDictionary alloc] initWithContentsOfFile: plistPath];
- (IBAction)Click_Segmentbutton:(id)sender {
switch (segment_Button.selectedSegmentIndex) {
case 0:{
break;}
case 1:{
break;}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSIndexPath *indexPath;
if (indexPath.section == 0)
{
return [[savedUrl objectForKey:#"boysinfo"] count];
}
else
{
return [[savedUrl objectForKey:#"girlsinfo"] count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Here I need to get whenever click the tableview cell selected cell all details
}
As per my understanding to your question, below is code for your purpose.
//Global
NSMutableDictionary *savedPlistDictionary;
- (void)viewDidLoad {
//Read the plist and save in plistDictionary.
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPathString = paths.firstObject;
NSString *plistNameString = #"second.plist"; //Second plist file
NSString *plistPathString = [documentsPath stringByAppendingPathComponent:plistNameString];
plistDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile: plistPathString];
}
//Function to fetch the plist file based on index (segment selected)
- (void)saveDataWithSelectedIndex:(int)index {
if (index == 1){
savedPlistDictionary = [plistDictionary objectForKey:#"boysinfo"];
}
else {
savedPlistDictionary = [plistDictionary objectForKey:#"girlsinfo"];
}
}
- (IBAction)Click_Segmentbutton:(id)sender {
//Pass the selected index segment to function and savedPlistDictionary will be updated accordingly
switch (segment_Button.selectedSegmentIndex) {
case 0:{
[self saveDataWithSelectedIndex:0];
break;}
case 1:{
[self saveDataWithSelectedIndex:1];
break;}
}
[self.tableView reloadData]; //Reload the tableView
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [savedPlistDictionary count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Here parse the data and load cells
}
This is my code for the UITableViews:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == _tblConnectedDevices){
return [_arrConnectedDevices count];
}
if ([filePathsArray count] > 0){
return [filePathsArray count];
}
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == _tblConnectedDevices){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CellIdentifier"];
}
cell.textLabel.text = [_arrConnectedDevices objectAtIndex:indexPath.row];
return cell;
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
NSString *last2 = [[last lastPathComponent] stringByDeletingPathExtension];
cell.textLabel.text = last2;
return cell;
}
}
At first launch, with the app not installed on the device, the application will crash with this error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
If I amend -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ to include:
if ([filePathsArray count] == 0){
return [filePathsArray count];
}
(I add the == 0 as well as > )
Then the app launches fine, but without displaying my UITableView correctly.
If I then close the app, and remove the added code, and launch it again, it works perfectly as expected.
What am I doing wrong/missing?
Thanks
Your code crashes when you're trying to access your array:
NSString *last = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
The error clearly says: index 0 beyond bounds for empty array.
So it will crash when indexPath.row = 0, since the numberOfRows method is incorrect:
if ([filePathsArray count] > 0){
return [filePathsArray count];
}
return 1;
This will return 1 when filePathsArray is empty. You should just drop the if and leave:
return [filePathsArray count];
I'm looking some mistakes, for example you never pass this if:
if (tableView == _tblConnectedDevices){
return [_arrConnectedDevices count];
}
You have to use isEqual: or compare tags...
I am trying to setup a UITableView that displays the data from the Documents Directory.
I am a bit lost for code, as I have tried many examples from Google and forums etc.
I am creating my app without Storyboard, so it is all in code.
I have got the UITableView displayed, so the delegates and DataView are set - I just need the content.
I have this code to show you, but it is not showing any data:
- (void)viewDidLoad
{
[super viewDidLoad];
_firstViewWithOutNavBar = [[UIView alloc] init];
_firstViewWithOutNavBar.frame = CGRectMake(self.view.frame.origin.x, 0, self.v iew.frame.size.width, self.view.frame.size.height);
_firstViewWithOutNavBar.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_firstViewWithOutNavBar];
UITableView *tableView = [[UITableView alloc] init];
tableView.frame = CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height);
tableView.delegate = self;
tableView.dataSource = self;
[_firstViewWithOutNavBar addSubview:tableView];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//alloc and init view with custom init method that accepts NSString* argument
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:indexPath.row];
NSString*pathToPass = [documentsDirectory stringByAppendingPathComponent:
[tableView cellForRowAtIndexPath:indexPath].textLabel.text]; //pass this.
NSLog(#"%#", pathToPass);
//_nsarray = [[NSArray alloc] initWithContentsOfFile:pathToPass];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_nsarray count];
NSLog(#"%#", _nsarray);
}
- (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];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#",[_nsarray objectAtIndex:indexPath.row]];
return cell;
}
Any help would be great.
The array _nsarray which is used as table view data source in numberOfRowsInSection and cellForRowAtIndexPath, is never initialized in your code. You should do something like
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
_nsarray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docPath error:NULL];
in viewDidLoad.
Did you place the breakpoints in numberOfRowsInSection method to check whether this method has been invoked or not. As I can see in your code you don't have initialize your _nsarray and didn't add any object in that array.
So basically your array contains 0 objects so no row will be created.
In your numberOfRowsInSection method You have placed the nslog after the return statement and this will never be executed please place it before the return statement so that you can actually see the array values.
I hope this will help you.
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;
}
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.