I've got 3 labels in a custom UITableview cell and I'm trying to pass in json data I've gotten from an api with Alamofire but I'm struggling to understand how to push the returned json into the tableview. Any help would be greatly appreciated.
Code below:
import UIKit
import Parse
import Alamofire
class LeagueTableController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, "https://api.import.io/store/connector/88c66c----9b01-6bd2bb--d/_query?input=webpage/url:----") .responseJSON { response in // 1
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
return cell
}
}
Returned json like this:
{
connectorGuid = "88c66cb4-e64f-4316-9b01-6bd2bb2d762d";
connectorVersionGuid = "8aedfe43-948a-4559-b279-d3c3c28047a4";
cookies = (
);
offset = 0;
outputProperties = (
{
name = team;
type = URL;
},
{
name = played;
type = DOUBLE;
},
{
name = points;
type = DOUBLE;
}
);
pageUrl = "http://www.extratime.ie/leagues/2024/100/premier-division/";
results = (
{
played = 9;
"played/_source" = 9;
points = 22;
"points/_source" = 22;
team = "http://www.extratime.ie/squads/17/";
"team/_source" = "/squads/17/";
"team/_text" = Dundalk;
},
{
played = 9;
"played/_source" = 9;
points = 20;
"points/_source" = 20;
team = "http://www.extratime.ie/squads/7/";
"team/_source" = "/squads/7/";
"team/_text" = "Derry City";
},
{
played = 9;
"played/_source" = 9;
points = 17;
"points/_source" = 17;
team = "http://www.extratime.ie/squads/100504/";
"team/_source" = "/squads/100504/";
"team/_text" = "Galway United FC";
},
{
played = 9;
"played/_source" = 9;
points = 16;
"points/_source" = 16;
team = "http://www.extratime.ie/squads/29/";
"team/_source" = "/squads/29/";
"team/_text" = "St. Patrick's Ath";
},
{
played = 8;
"played/_source" = 8;
points = 15;
"points/_source" = 15;
team = "http://www.extratime.ie/squads/30/";
"team/_source" = "/squads/30/";
"team/_text" = "Cork City";
},
{
played = 8;
"played/_source" = 8;
points = 15;
"points/_source" = 15;
team = "http://www.extratime.ie/squads/3/";
"team/_source" = "/squads/3/";
"team/_text" = "Shamrock Rovers";
},
{
played = 9;
"played/_source" = 9;
points = 10;
"points/_source" = 10;
team = "http://www.extratime.ie/squads/13/";
"team/_source" = "/squads/13/";
"team/_text" = "Finn Harps";
},
{
played = 9;
"played/_source" = 9;
points = 10;
"points/_source" = 10;
team = "http://www.extratime.ie/squads/2/";
"team/_source" = "/squads/2/";
"team/_text" = Bohemians;
},
{
played = 9;
"played/_source" = 9;
points = 7;
"points/_source" = 7;
team = "http://www.extratime.ie/squads/8/";
"team/_source" = "/squads/8/";
"team/_text" = "Sligo Rovers";
},
{
played = 9;
"played/_source" = 9;
points = 7;
"points/_source" = 7;
team = "http://www.extratime.ie/squads/6/";
"team/_source" = "/squads/6/";
"team/_text" = "Bray Wanderers";
},
{
played = 9;
"played/_source" = 9;
points = 5;
"points/_source" = 5;
team = "http://www.extratime.ie/squads/109/";
"team/_source" = "/squads/109/";
"team/_text" = "Wexford Youths";
},
{
played = 9;
"played/_source" = 9;
points = 5;
"points/_source" = 5;
team = "http://www.extratime.ie/squads/15/";
"team/_source" = "/squads/15/";
"team/_text" = "Longford Town";
}
);
}
I'm trying to just push the "played", "points" and "team/_text" results out to each of the labels.
Since the question is very broad and doesn't specify what exactly is the problem here, The general steps are:
1) map your json to dictionary/nsdictionary. Suppose the JSON snippet you posted is a chunk of JSONArray in following format [{}], all you need to do is:
var arrayOfDictionaries:NSArray = NSJSONSerialization.JSONObjectWithData(yourData, options: nil, error: nil) as! NSArray
where yourData variable is data downloaded from network casted to NSData format
2) create outlets to these three labels in your custom tableViewCell
3) for each cell, set these labels inside
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
method as follows:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:YourCustomCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as YourCustomCell
cell.firstLabel.text = yourDictionary["played"]
cell.secondLabel.text = yourDictionary["points"]
cell.thirdLabel.text = yourDictionary["team"]
return cell
}
4) I suppose you will need more cells than just one, then store many dictionaries in array and access each element like this:
cell.firstLabel.text = arrayOfDictionaries[indexPath.row]["played"]
You'll need to create a subclass of UITableViewCell, say MyTableViewCell and add a property named JSON.
Now, since you're probably using Interface Builder to define your cell and its reuse identifier ("Cell"), set that cell's class to your newly created MyTableViewCell and connect the labels to some IBOutlets in your newly defined class.
Then, when you call 'dequeueReusableCellWithIdentifier', cast the cell to MyTableViewCell and set its JSON property to the value you want to have in the cell.
You'll probably want to react to the change, so add the didSet property observer.
var JSON:[String: AnyObject] = [String: AnyObject]() {
didSet {
print("populate your labels with your new data");
}
}
First, you should create a model that contain 3 properties that you want to save, like:
class Data {
var team = ""
var point = 0
var teamText = ""
init(fromJSON json: NSDictionary) {
team = json["team"] as! String
point = json["points"] as! Int
teamText = json["team/_text"] as! String
}
}
In your LeagueTableController, create an array to hold the data and show it to tableView:
var data = [Data]()
Config the tableView to show the data:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! YourCustomCell
let row = indexPath.row
cell.lblA.text = data[row].team
cell.lblB.text = "\(data[row].point)"
cell.lblC.text = data[row].teamText
return cell
}
Finally, parse your response json to our data array to display onto tableView
#IBOutlet weak var tableView: UITableView!
var data = [Data]()
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, "https://api.import.io/store/connector/88c66c----9b01-6bd2bb--d/_query?input=webpage/url:----") .responseJSON { response in // 1
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
// parse JSON to get array of data
let array = JSON["results"] as! [NSDictionary]
// map an item to a data object, and add to our data array
for item in array {
self.data.append(Data(fromJSON: item))
}
// after mapping, reload the tableView
self.tableView.reloadData()
}
}
}
Hope this help!
Related
I am getting instagram post response but from that i am getting array into object i mean main array is data and in that i ahve object user and in that i want to print full_name Here is response
Response
SUCCESS: {
data = (
{
attribution = "<null>";
caption = {
"created_time" = 1533621008;
from = {
"full_name" = "Mike Alpha";
id = 8376082973;
"profile_picture" = "";
username = mikealpha607;
};
id = 17952863989106568;
text = Hi;
};
comments = {
count = 1;
};
"created_time" = 1533621008;
filter = Normal;
id = "1840468872962982375_8376082973";
images = {
"low_resolution" = {
height = 320;
url = "";
width = 320;
};
"standard_resolution" = {
height = 640;
url = "https://scontent.cdninstagram.com/vp/adcc206df693d4d65c9b6f6b5ef7a016/5C024385/t51.2885-15/sh0.08/e35/s640x640/37867466_267564047306571_7531604489342550016_n.jpg";
width = 640;
};
thumbnail = {
height = 150;
url = "";
width = 150;
};
};
likes = {
count = 2;
};
link = "https://www.instagram.com/p/BmKqLBRH0Hn/";
location = {
id = 761100677398542;
latitude = "23.102652628869";
longitude = "72.595614227203";
name = "PVR Cinemas, Motera, Ahmedabad";
};
tags = (
);
type = image;
user = {
"full_name" = "Mike Alpha";
id = 8376082973;
"profile_picture" = "";
username = mikealpha607;
};
"user_has_liked" = 1;
"users_in_photo" = (
);
}
and i am getting user object but no understand that how to print on label let me show my code
Code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TestTableViewCell
let createdTime = serviceData[indexPath.row]["created_time"]
let timeinterval : TimeInterval = (createdTime as! NSString).doubleValue
let dateFromServer = NSDate(timeIntervalSince1970:timeinterval)
let dateFormater : DateFormatter = DateFormatter()
dateFormater.dateFormat = "dd MMM yyyy"
cell.lblDate.text = dateFormater.string(from: dateFromServer as Date)
let instaResponse = serviceData[indexPath.row]["user"]
print(instaResponse) **Here I am getting user object response but i understand how to print on label**
return cell
}
please help me to under stand this
please check this new issue
let imagePosts = serviceData[indexPath.row]["images"] as! [String:Any]
let instaImage = imagePosts["standard_resolution"]
let proimgurl1 = NSURL(string: instaImage as! String)
cell.imgPost.image = UIImage(data: NSData(contentsOf: proimgurl1! as URL)! as Data)
i am geting imageurl but when i pass to image view its going crash
You can try
let instaResponse = serviceData[indexPath.row]["user"] as! [String:Any]
let fName = instaResponse["full_name"] as! String
cell.fnameLbl.text = fName // suppose outlet is fnameLbl
printfName)
//
let imagePosts = serviceData[indexPath.row]["images"] as! [String:[String:Any]]
let instaImage = imagePosts["standard_resolution"]["url"] as! String
To get data the from json class is created
class getData: NSObject {
var descriptionn : String = ""
var image : String = ""
// static let shared = getData()
func getDataForTableView(results: [[String:String]], index : Int){
var productArray = [String:String]()
productArray = results[index]
descriptionn = productArray["description"]!
image = productArray["images"]!
}
}
To display data in table view
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryNewscell") as! DiscoveryNewsTableViewCell
// if results.count > 0{
classObject.getDataForTableView(results: results, index: indexPath.row)
cell.sneakerImageView.image=filteredsneakernews[indexPath.row].image
print("abc image"+classObject.image)
cell.newsTitle.text = classObject.descriptionn
// }
return cell
}
The json is following foramt
ptional({
data = (
{
"created_date" = "2017-11-09 14:58:58";
"created_on" = "2017-11-09";
"createdd_by" = 3;
description = dfdfdsdfsdfs;
id = 4;
images = "7a53f87882409c4622a0977d5026038b.jpg";
likes = 25;
product = 12;
status = 1;
title = facebook;
"title_keys" = fac;
type = News;
},
{
"created_date" = "2017-11-15 14:33:01";
"created_on" = "2017-11-23";
"createdd_by" = 3;
description = dfdfdf;
id = 7;
images = "e626b8003e6e08df874e33556e7f6e69.jpg";
likes = 3;
product = 0;
status = 1;
title = don;
"title_keys" = don;
type = News;
},
{
"created_date" = "2017-11-16 10:34:48";
"created_on" = "2017-11-13";
"createdd_by" = 3;
description = "my first computer";
id = 8;
images = "556b1855de039b8d99bc787acd103262.jpg";
likes = 90;
product = 13;
status = 1;
title = Dell;
"title_keys" = dell;
type = News;
},
{
"created_date" = "2018-01-02 16:23:54";
"created_on" = "2018-01-08";
"createdd_by" = 3;
description = sdfsdfsfdf;
id = 14;
images = "0c980d3f9cd0393a46bb04995d16177a.jpg";
likes = 0;
product = 0;
status = 1;
title = dfsfdsfdfs;
"title_keys" = " dfsfdsfdfs";
type = News;
}
);
message = "Success.";
newsurl = "http://dev.nsol.sg/projects/sneakers/assets/brand_images/thumb/";
success = 1;
})
This json has image of following from
images = "7a53f87882409c4622a0977d5026038b.jpg";
How to display the image .Image(classObject.image) in string format how to display image view on table view ?you can download the code from this link .https://drive.google.com/file/d/1bVQsuSQINSa6YRwZe2QwEjPpU_m7S3b8/view?usp=sharing
Image path should be like URL format. http://dev.nsol.sg/projects/sneakers/assets/brand_images/thumb/7a53f87882409c4622a0977d5026038b.jpg.
yourJSON
message = "Success.";
newsurl = "http://dev.nsol.sg/projects/sneakers/assets/brand_images/thumb/";
success = 1;
Global Declaration:
var imgURLStringArr = [String]()
func appendURLPathFromJSON(jsonDict: NSDictionary)
{
let urlPath : String = jsonDict.value(forKey: "newsurl") as! String
let dataArr : NSArray = jsonDict.value(forKey: "data") as! NSArray
for i in 0..<dataArr.count
{
let dataDict : NSDictionary = dataArr[i] as! NSDictionary
let imageStr : String = dataDict.value(forKey: "images") as! String
let appendPath : String = urlPath + imageStr
imgURLStringArr.append(appendPath)
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print("willDisplaywillDisplay")
let cell = cell as! DiscoveryNewsTableViewCell
URLSession.shared.dataTask(with: NSURL(string: self.imgURLStringArr[indexPath.row])! as URL, completionHandler: { (data, response, error) -> Void in
if error != nil {
print(error ?? "No Error")
return
}
DispatchQueue.main.async(execute: { () -> Void in
let image = UIImage(data: data!)
cell.thumbImg.image = image
})
}).resume()
}
In a viewController I've connected a tableView outlet in the SB with delegates and datasource set and when I'm accessing the reference outlet in code it always crashes with the message:
fatal error: unexpectedly found nil while unwrapping an Optional value
Here's the code:
#IBOutlet var resultTabView: UITableView!
Connection:
cellForRowAtIndexPath: -
I'm not even using resultsArray, though the array is returning data as seen in the logs
EDIT 2:
Data:
id = 2325;
results = (
{
city = Delhi;
company = "AIR CREATOR";
country = IN;
date = "Mon, 11 Jul 2016 04:51:34 GMT";
expired = 0;
formattedLocation = "Delhi, Delhi";
formattedLocationFull = "Delhi, Delhi";
formattedRelativeTime = "30+ days ago";
jobkey = 67ba422cc328a946;
jobtitle = "CSA for airport";
latitude = "28.664835";
longitude = "77.21429000000001";
onmousedown = "indeed_clk(this,'3586');";
snippet = "Providing boarding passes and luggage <b>labels</b>. Your daily tasks will usually include:....";
source = Indeed;
sponsored = 0;
state = DL;
url = "http://www.indeed.co.in/viewjob?jk=67ba422cc328a946&qd=SHSUhPNSBYpqaNu9AVdfqv81wDpAwFE36uyNVXcxl8VAUThqIJkZA1l3w1yQl8AJg05AJq_aODrwRlOUKXT92UBk4SDqDqM7LhTReglYsV0&indpubnum=4148406673078092&atk=1aqbfaj6kb958c17";
},
{
city = Delhi;
company = "Letmedrive India Private Limited";
country = IN;
date = "Wed, 10 Aug 2016 13:02:51 GMT";
expired = 0;
formattedLocation = "Delhi, Delhi";
formattedLocationFull = "Delhi, Delhi";
formattedRelativeTime = "6 days ago";
jobkey = 89c4f63f1b841720;
jobtitle = "Graphics Designer";
latitude = "28.664835";
longitude = "77.21429000000001";
onmousedown = "indeed_clk(this,'3586');";
snippet = "Graphic designers/artists design graphics for use in media products such as magazines, <b>labels</b> & advertising....";
source = Indeed;
sponsored = 0;
state = DL;
url = "http://www.indeed.co.in/viewjob?jk=89c4f63f1b841720&qd=SHSUhPNSBYpqaNu9AVdfqv81wDpAwFE36uyNVXcxl8VAUThqIJkZA1l3w1yQl8AJg05AJq_aODrwRlOUKXT92UBk4SDqDqM7LhTReglYsV0&indpubnum=4148406673078092&atk=1aqbfaj6kb958c17";
};
Class:
class JobListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate{
#IBOutlet var searchField: UITextField!
#IBOutlet var resultTabView: UITableView!
var resultsArray = [JSON]()
var resultsDictionary = [String:String]()
override func viewDidLoad() {
super.viewDidLoad()
}
func focusSearchField(search:Bool, keyword:String){
if(search){
callAPI(keyword)
}else{
searchField?.becomeFirstResponder()
}
}
func callAPI(keyword:String){
var params = [String:String]()
params["publisher"] = "2341411"
params["q"] = keyword
params["l"] = "dubai"
params["latlong"] = "1"
params["v"] = "2"
params["format"] = "json"
params["co"] = "uae"
let svcMgr = ServiceManager()
svcMgr.httpRequest(self, params: params)
}
func updateUI(result: [String:AnyObject]){
if let val = result["results"] {
let json = JSON(val)
let array = json.arrayValue
dispatch_async(dispatch_get_main_queue(), {
for a in array{
self.resultsArray.append(a)
}
self.resultTabView.reloadData()
})
}else{
}
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return resultsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("JobCell", forIndexPath: indexPath) as UITableViewCell
let labl2 = cell.viewWithTag(2) as! UILabel
labl2.text = (resultsArray[indexPath.row]["jobtitle"].stringValue)+" #"+(resultsArray[indexPath.row]["source"].stringValue)
let labl3 = cell.viewWithTag(3) as! UILabel
labl3.text = (resultsArray[indexPath.row]["state"].stringValue)+", "+(resultsArray[indexPath.row]["country"].stringValue)
let labl4 = cell.viewWithTag(4) as! UILabel
labl4.text = (resultsArray[indexPath.row]["snippet"].stringValue)+" "
tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
UIApplication.sharedApplication().openURL(NSURL(string: resultsArray[indexPath.row]["url"].stringValue)!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - TextField delegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
callAPI(textField.text!)
return true
}
Follow the following steps,
Please remove all connection with table view and add that again.
Make sure tableViewCellIdentifier have same name set in storyboard as given in code.
remove "!" mark after AnyObject, i beleive you need this
var resultsArray = (val as? [AnyObject])
This is not table view reloading error, its parsing issue. Please check all your declare properties.If possible show all your table view data source methods.
Make sure the viewDidLoad method is called before attempting to access the table view. Your connection from XIB is probably not established by the time this code runs.
This is my first post and I hope its a great question because iv been stuck on this for days. (Literally searched every related question and found nothing that I could add up for a solution.)
Basically I'm building a pure Swift application. My problem is that I cant figure out how to place each dictionary values into each UITableCell that is created.
Also the JSON response from the GET creates a NSCFDictionary type.
UITableViewCell Properties
UILabel - Holds the Offer "name"
UI Label #2 - Holds Offer "description"
UIImage - Holds the Offer "thumb_url"
So basically I need to store each offer object's (name, description, thumb_url) in every UITableViewCell that the ViewController creates.
GET Request
import UIKit
class freeIAPCell: UITableViewCell {
#IBOutlet weak var aName: UILabel!
#IBOutlet weak var aDescription: UILabel!
#IBOutlet weak var aImage: UIImageView!
}
class FreeIAPViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// Everbadge Request
var apiURL = "apiurlhere"
var parsedArray: [[String:String]] = []
func queryEB() {
// Query DB Here
// Store the API url in a NSURL Object
let nsURL = NSURL(string: apiURL)
let request = NSMutableURLRequest(URL: nsURL!)
request.HTTPMethod = "GET"
// Execute HTTP Request
let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL!) {
data, response, error in
if error != nil {
print("Error = \(error)")
return
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! NSDictionary
print(json.isKindOfClass(NSDictionary)) // true
let data = json.objectForKey("data") as! NSDictionary
//print(data)
let m = data.objectForKey("offers") as! NSArray
print(m)
print(m.valueForKeyPath("name"))
self.parsedArray = m as! [[String : String]]
} catch {
print("THERE WAS AN ERROR PARSING JSON FOR EVERBADGE")
}
}
task.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
queryEB()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
}
// MARK: UITableView method implementation
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("freeAppCell", forIndexPath: indexPath) as! freeIAPCell
let obj = parsedArray[indexPath.row] // fatal error: Index out of range
cell.aName.text = obj["name"]
return cell
}
}
API Request Data Structure (print(m))
(
{
"name" = "Mate";
id = 23941;
description = "10-20 word description goes here"
"url" = "google.com
"ver" = "0.12"
price = "0.17"
"public_name" = "Good Ole Mate"
"thumb_url" = "http://google.com/mate.jpg
};
{
"name" = "Mate";
id = 23941;
description = "10-20 word description goes here"
"url" = "google.com
"ver" = "0.12"
price = "0.17"
"public_name" = "Good Ole Mate"
"thumb_url" = "http://google.com/mate.jpg
};
{
"name" = "Mate";
id = 23941;
description = "10-20 word description goes here"
"url" = "google.com
"ver" = "0.12"
price = "0.17"
"public_name" = "Good Ole Mate"
"thumb_url" = "http://google.com/mate.jpg
};
{
"name" = "Mate";
id = 23941;
description = "10-20 word description goes here"
"url" = "google.com
"ver" = "0.12"
price = "0.17"
"public_name" = "Good Ole Mate"
"thumb_url" = "http://google.com/mate.jpg
};
{
"name" = "Mate";
id = 23941;
description = "10-20 word description goes here"
"url" = "google.com
"ver" = "0.12"
price = "0.17"
"public_name" = "Good Ole Mate"
"thumb_url" = "http://google.com/mate.jpg
};
);
First create public arrays for your 3 items...
var nameArray = [String]()
var descriptionArray = [String]()
var thumbnailArray = [String]()
Then loop through your json parse like this....
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) as! NSDictionary
if responseString != nil
{
let items: AnyObject! = responseString["items"] as AnyObject!
if items != nil
{
// Loop through all search results and keep just the necessary data.
for var i=0; i<items.count; ++i
{
if let names = items[i]["name"] as? NSDictionary
{
let name = names as! String
self.nameArray.append(name)
}
if let descriptions = items[i]["description"] as? NSDictionary
{
let description = descriptions as! String
self.descriptionArray.append(description)
}
if let thumbnails = items[i]["thumb_url"] as? NSDictionary
{
let thumbnail = thumbnails as! String
self.thumbnailArray.append(thumbnail)
}
}
}
self.resultsTableView.reloadData()
Create an OfferTableViewCell Class with a nameLabel:UILabel, descriptionLabel:UILabel, and thumbnailImage:UIImage. Finally in your cellForRowAtIndexPath.....
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("offer", forIndexPath: indexPath) as! OfferTableViewCell
cell.nameLabel.text = self.nameArray[indexPath.row]
cell.descriptionLabel.text = self.descriptionArray[indexPath.row]
let url = NSURL(string: self.thumbnailArray[indexPath.row])
let data = NSData(contentsOfURL: url!)
cell.thumbnailImage.image = UIImage(data: data!)
return cell
}
i have one table view, which will dipslay the json data from one url.Its all working fine. What i need is?. in my table view i have one lable called "name label ".Which will display the name in my table view.
And i have one menu option with one button name called" sort data A - z ".When i click that, my table view data should reload with sorting the date from A-z alphabets order ( My name title ).
This is my button action :
#IBAction func sortByAZBtnPress(sender: AnyObject) {
}
My viewcontroller.swift
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var RightMenu: UIView!
#IBOutlet weak var tableView: UITableView! // UITable view declaration
#IBOutlet weak var Resultcount: UILabel! // count label
var arrDict :NSMutableArray=[] // array to store the value from json
let cellSpacingHeight: CGFloat = 5 // cell spacing from each cell in table view
override func viewDidLoad() {
super.viewDidLoad()
self.jsonParsingFromURL() // call the json method
let nib = UINib(nibName:"customCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
RightMenu.layer.borderWidth = 1
}
// web services method
func jsonParsingFromURL () {
let url = NSURL(string: "http://sampleUrl.com”)
let session = NSURLSession.sharedSession()
let request = NSURLRequest(URL: url!)
let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
// print("done, error: \(error)")
if error == nil
{
dispatch_async(dispatch_get_main_queue()){
self.arrDict=(try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSMutableArray
//print(self.arrDict)
if (self.arrDict.count>0)
{
self.Resultcount.text = "\(self.arrDict.count) Results"
self.tableView.reloadData()
}}
// arrDict.addObject((dict.valueForKey("xxxx")
}
}
dataTask.resume()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.arrDict.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// calling each cell based on tap and users ( premium / non premium )
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:customCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! customCell
cell.vendorName.text=arrDict[indexPath.section] .valueForKey("name") as? String
cell.vendorAddress.text=arrDict[indexPath.section] .valueForKey("address") as? String
return cell
}
// MARK:
// MARK: Sort Method
#IBAction func sortByRevBtnPress(sender: AnyObject) {
self.indicator.startAnimating()
self.indicator.hidden = false
RightMenu.hidden = true
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(1 * NSEC_PER_SEC)), dispatch_get_main_queue()){
self.indicator.stopAnimating()
self.indicator.hidden = true
};
self.tableView.reloadData()
}
#IBAction func sortByAZBtnPress(sender: AnyObject) {
}
}
Please help me out. I am not getting clear idea to do that...Thanks
Updated :
(
{
ad = 0;
address = "900 York Mills Rd, Toronto, ON M3B 3H2, Canada";
"category_id" = 1;
latitude = "43.7563";
longitude = "-79.3495";
name = "Honeybee Restaurant";
phone = 9876543210;
rating = "5.0";
},
{
ad = 1;
address = "1677 Wilson Ave, Toronto, ON M3L 1A5, Canada";
"category_id" = 1;
latitude = "43.7194";
longitude = "-79.5153";
name = "Maki My Way";
phone = 9875463254;
rating = "4.0";
},
{
ad = 1;
address = "75 Bremner Blvd, Toronto, ON M5J 0A1, Canada";
"category_id" = 1;
latitude = "43.6429";
longitude = "-79.3814";
name = "Blow Fish Restaurant";
phone = 9873245610;
rating = "5.0";
},
{
ad = 0;
address = "4150 Yonge St, Toronto, ON M2P 2C6, Canada";
"category_id" = 1;
latitude = "43.747";
longitude = "-79.4079";
name = "SaigonFlower Restaurant";
phone = 7892345621;
rating = "3.0";
},
{
ad = 1;
address = "604 King St W, Toronto, ON M5V 1M6, Canada";
"category_id" = 1;
latitude = "43.6445";
longitude = "-79.4004";
name = "Sushi Gen";
phone = 7456321065;
rating = "2.0";
},
)
Assuming that your array is an array of dictionaries and your dictionary has a "Name" key you can do this. Change the code if your dict has different keys that contains the name.
#IBAction func sortByAZBtnPress(sender: AnyObject) {
arrDict.sortUsingComparator { (dict1, dict2) -> NSComparisonResult in
if let name1 = dict1["name"] as? String, name2 = dict2["name"] as? String {
return name1.compare(name2)
}
return .OrderedAscending
}
self.tableView.reloadData()
}
For order by rating (High to low) you can use this.
arrDict.sortUsingComparator { (dict1, dict2) -> NSComparisonResult in
if let name1 = dict1["rating"] as? String, name2 = dict2["rating"] as? String {
return name2.compare(name1)
}
return .OrderedAscending
}
For sorting of the tableview you should sort youre dataSource firstly and than reload tableview.
In you're case:
Firstly sort by A to z:
arrDict
For sorting use functional programming:
arrDict = arrDict.sort({ $0.name > $1.name })
Secondly reload tableview by:
self.tableView.reloadData()
sort arrDict using below code then self.tableView.reloadData():
sortedArray = [anArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];