-2

I currently have a list of 1,000 records that I would like to use to populate into a Tableview but I am not sure how to go about doing so. The list is currently in a .txt file. Does anyone have any suggestions as to how to go about populating the table view with this list. Any suggested line of codes would be greatly appreciated.

EDIT: Updating initial question to provide more detail.

Say I have a list of all of the countries in the world that I want to load into a tableview. I have the list in a text file. Is the best approach to insert all of the countries one by one into an array or can I just link the data source of the table view to the .txt file to load the data? Maybe there is another way that I cannot think of. (Sorry if this is not enough detail, I am new to Objective C)

ObjCNoob
  • 1
  • 1
  • You would to have serious formatting to get it so that your program could tell when one line starts and another ends. – Wyetro Aug 05 '14 at 17:17
  • 1
    Please read about uitableview data sources, write your own "lines of code", and see if they work. If they do not, post your code, and ask a specific question on how to improve it. – Sergey Kalinichenko Aug 05 '14 at 17:19
  • Your question is a too broad and vague. What is the exact issue you are having? – rmaddy Aug 05 '14 at 17:19
  • Do you have any suggestions how I can populate a table with a list without having to write all of the values one by one into an array? – ObjCNoob Aug 05 '14 at 17:19
  • `NSArray *source = [textString componentsSeparatedByString:@"yourDelimiter"];` – Stonz2 Aug 05 '14 at 17:36
  • No, you cannot link a tableview directly to a .txt file. You first have to store, access, and parse the file. You then turn each record into an appropriate Obj-C data type that will subsequently be put into the tableview. If all you need is country names, you can use methods already suggested below for creating an array by splitting on each new line or other delimiter. If your records are more complex with multiple parts, you may want to first convert the data to a common format such as XML, JSON, or a plist, then investigate how to work with those in iOS. – mc01 Aug 05 '14 at 17:40

1 Answers1

0

Just read the file using NSFileManager. Once you have the file into a single string object, then you can check this answer in how to split an string obtained from a file.

How to split newline from NSString in ObjectiveC

Then once you have this into an array you can fill your tableView really easy using the UITableViewDataSource Delegate Method

cellForRowAtIndexPath:

just assign to the cell.textLabel.text the value for the array at index indexPath.row provided by the method.

cellForRowAtIndexPath:

If you can provide some code and the example in how your file looks you could receive more help. Your questions is vague but what I just answer might help to clarify your ideas

Community
  • 1
  • 1
artud2000
  • 544
  • 4
  • 9
  • You can't use `NSFileManager` to load the file into an `NSString`. – rmaddy Aug 05 '14 at 17:45
  • Thanks. I will try that. Sorry for being vague. I have trouble explaining what I am trying to accomplish. – ObjCNoob Aug 05 '14 at 17:49
  • He can get the path and read it into an string using NSFileManager rmaddy – artud2000 Aug 05 '14 at 18:00
  • @artud2000 You might get the path using `NSFileManager` (and even that's not likely) but you certainly don't use `NSFileManager` to actually load the file into the string. Please point out the `NSFileManager` method for loading a file into an `NSString`? – rmaddy Aug 05 '14 at 18:19
  • NSString *filePath = [[NSBundle mainBundle] pathForResource:@"path obtained from NSFileManager" ofType:@"txt"]; NSString *testString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; I probably wasn't clear but you get path with NSFileManager check if the file exists and then you can get the string @rmaddy – artud2000 Aug 05 '14 at 18:27