iOS - get JSON feed in every tableview cell with Swift - ios

I'm building an iOS app with a table view. I have an array containing ID's that I use to populate the table view. Now I want to do a call to a web service in each table view cell that contains this ID in the URL. I want to use the JSON response to fill the cell with the correct data. I think the data has to be fetched in the background.
Can anyone point me in the right direction of how I can do this with Swift? I searched the web but I couldn't find any tutorials about this.

I think help full this step for you.
Step 1 :- Call API service from viewDidLoad() function.
Step 2 :- Handle API response and Use one array for store API data
Step 3 :- array count use for numberOfRowsInSection
Step 4 :- Use array data in cellForRowAtIndexPath

Related

Swift 2.0: How to implement tableview controller with JSON API

I think this is a probably repeat question.
I am new to ios and and I am trying to learn so I don't expect response in actual codes. I still want to write them myself.
Here's what I have done so far:
I wrote codes to make web api calls, authenticate and pull json data.
I've figured out how to parse json data that I pulled from web.
I have made tableview controller.
Challenge: How do I get the parsed Json data to tableview controller?
Do I:
- save it locally into like NSUSERDefaults variable?
- save it to a global variable shared by controller and my model?
- at the end of the parsing json data do a return so that it will be saved into viewController?
- or none of the above and do ______________ (insert your answer)
What's the cleanest way to do this? The table will initially have like 20 rows but as you pull down you will get more rows.
Best way to save data on model class while parsing and store this class objects in an array and in cellforrowatindexpath get objects of model class from array and show data using these objects.

do tableView.reloadData() in external class

I am doing a Parse query from an external class. This query give me back the array for populate the tableview, so in the viewDidLoad method I call MyClass.load(array) and the async parse method findObjectInBackground return me the array filled but in the time that the findObject retrive me the objects the tableView is already created and I see an empty tableView, I did a fast google search for the problem and I found that I have to use the self.tableView.reloadData() method, I tried it but I am in an external class and the delegate for the tableView is in the tableViewController, there is any way to refresh the tableView from an external Class?
If you need some code example just ask it, thank you!
EDIT I'm using Swift 2.0
You need to set custom delegate between your external class and ViewControllers.
check for creating custom delegate
Also, you can use NSNotificationCenter for send and post notifications. please check link

Swift - Updating a UITableView with remote JSON data

i am trying to access data from a remote json file. When something is typed into the searchbar a new NSURLSessionDataTask is made to access the json file. I have successfully been able to extract all the data i need from the json file, but have not been able to update the table view as soon as the data comes in. To show the data i must either scroll so one cell is not visible on the screen. This will make it so that only that one cell has the correct values in it, alternatively, i can also type in a other letter into the searchable which will generate a new json file, but the data from the previous request is only shown now.
I have tried updating the table view in the main thread but that doesn't seem to make any difference. I have also tried multiple other methods, but still no luck.
You can try like this
if let resData = swiftyJsonVar["loginNodes"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
if self.arrRes.count > 0 {
self.tableView.reloadData()
}
Note :arrRes is the name of array.
complete details are :How get JSON Data in Swift and add it to URL
To get the best answer you'll want to post code, specifics, and maybe even a link to the full project, but based on what you are saying the data is loaded in the code properly and is not being refreshed in the table view.
To fix this you first need a reference to the table view that is not loading, for this example I'll call it tableView. Then you can force it to reload all the visible data by calling
tableView.reloadData()
After your JSON data has finished loading.
When your data is loaded successfully just call
self.table.reloadData()
What it will do is it will populate the cells again with the updated data.

iOS Swift - TableView data source - saving data when app close

I'm working on an swift application that uses a TableView. It's cells are filled-up with data that comes from a remote server. That's how my app works now, but I want to change this mode because I'm sure that it's not the correct way to do things:
First: when the tableview did load, I call a function that get data (json) from server with this method:
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
After that, I save my data as an object in NSUserDefaults. The object contains strings such as username, description, location, picture... etc. I know that this is not correct to store images in NSUserDefaults even if these images are not so big...
Second: I fill up table's cells with data obtained from NSUserDefauts, where I have stored my json data.
First of all I want to know how to auto append data to the table when the user hits the last cell of the table, like all apps that uses tableviews for fetching user posts...
In Instagram, after the you close the app, disconnect from the internet, and reenter the app, it will show you the first 10 cells from the last table you've seen before closing the app, so Instagram saves somehow, somewhere, maybe in coredata, data for the first 10 cells.
How do you suggest me to do this?
Question is not particular.....
Load Data error.
Or you fetch the result from coreData error?
CoreData can save View and some informations.
Post your code.

list to NSIndexPath monotouch

I am trying to add records dynamically to an already populated UITable. In my UITableView class I have a custom List object
List<CustomObject> _data=new List<CustomObject>();
where I add records to from my webservice call like so:
_data.Add(Json(Object));// Json returns a list of CustomObject
//table is a UITableView
//this.table.InsertRows(_data);
I know this above line won't work because its expecting an NSIndexPath[] but I am not sure how to go about adding the new items an load only the new items added. I don't really follow objective C very well so examples I found don't help so much
Generally, you will add data to your List, and then call ReloadData() on your TableView. You usually only call InsertRows() if you want to insert data at a particular spot in the table based on some sort of user action.
You are trying to add a List of CusomObject as CusomObject to a List<CusomObject> ,that is not quite right.you should be adding a CusomObject to List<CusomObject>.

Resources