0

So basically i what i am trying to do is getting all the file names in a folder and put the Data in a UITableView, UIPickerView or Whatever you guys can code..

Read Dir (Get Filenames):

NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray *files = [fileManager contentsOfDirectoryAtPath:@"/Library/PreferenceLoader/Preferences/" error:nil];

NSString *Apps_Listed = [files description];

[Apps_List setText:Apps_Listed];

the Apps_List is a UITextView and the result is:

( "CallBarPreferences.plist", "KillBackgroundPreferences.plist", "PulltoRefreshMailSettings.plist", "doubleAtSettings.plist" )

What i need 2 do whith this result is put each " " in a UITableViewCell or UIPickerView, the only problem is i dont know how to make any of those work, been searching online 4 a while now, but cant seem to find a short and easy code that simply displays data ! ...

Hope someone can help me out here, please leave some code 4 me 2 work with.. Thanks.

Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
  • Go and read raywenderlich.com, he has very nice examples of exactly that. – Kheldar Aug 17 '11 at 09:12
  • Do not use the description method, it is not designed to be displayed to the user and may change in the future. In this case files is already an array of file names. – zaph Aug 17 '11 at 11:48

1 Answers1

1

To add your data to UITableView you have to tell your class you want to use UITableViewDelegate and UITableViewDataSource functions, then add those functions in your code.

In your header file add DataSource and Delegate to your interface:

@interface myTableViewController : UIViewController <UITableViewDataSource ,UITableViewDelegate>

In your implementation file add, these functions:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return [files count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:@"cell"];

// create a cell
if ( cell == nil)
{
    cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault 
                         reuseIdentifier:@"cell"];
}

// fill it with contents
cell.textLabel.text = [files objectAtIndex:indexPath.row];

// return it
return cell;

}
Hadi Sharghi
  • 903
  • 16
  • 33
  • I did what you sent me, but the UITableView stays empty, any ideas ? – Aleksander Azizi Aug 17 '11 at 10:41
  • I got it working, needed: MyTableView.delegate = self; MyTableView.dataSource = self; in startup. – Aleksander Azizi Aug 18 '11 at 10:53
  • Thanks for vote up. Sorry, I forgot to tell you to make the connections. What you did by code in start-up, you could do that it IB. Select the UITableView, in Connections Inspector windows, drag dataSource and delegate to File's Owner – Hadi Sharghi Aug 19 '11 at 18:12
  • I know, but i was making an empty-window based app, so it crashed when i used Connections .. :/ (iOS 5 SDK) – Aleksander Azizi Aug 19 '11 at 21:48
  • Need help with something simulare to this: http://stackoverflow.com/questions/7128032/iphone-sdk-xcode-4-put-ipod-mediaitemcollection-data-in-uitableview – Aleksander Azizi Aug 19 '11 at 22:24