import UIKit
class ViewController: UITableViewController {
var entries =import NSMutableArray()UIKit
class ViewController: UITableViewController {
var entries = NSMutableArray()
let newsUrlStrings = [
"http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://www3.nhk.or.jp/rss/news/cat0.xml&num=8",
"http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://www3.nhk.or.jp/rss/news/cat6.xml&num=8",
"http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://www3.nhk.or.jp/rss/news/cat7.xml&num=8"
]
let imageNames = [
"japan.jpg",
"world.jpg",
"sport.jpg",
]
let imageNames = [
"japan.jpg",
"world.jpg",
"sport.jpg",
]
@IBAction func refresh(sender: AnyObject) {
entries.removeAllObjects()
for newsUrlString in newsUrlStrings{
var url = NSURL(string: newsUrlString)!
var task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { data, respinse, error in
var dict@IBAction =func NSJSONSerialization.JSONObjectWithDatarefresh(data, options: NSJSONReadingOptions.MutableContainers, errorsender: nilAnyObject) as! NSDictionary
if var responseData = dict["responseData"] as? NSDictionary {
if var feed = responseData["feed"] as? NSDictionary {entries.removeAllObjects()
if var entries = feed["entries"]for as?newsUrlString NSArrayin newsUrlStrings{
var formatterurl = NSDateFormatterNSURL(string: newsUrlString)!
var formatter.localetask = NSLocaleNSURLSession.sharedSession(localeIdentifier: "en-US")
formatter.dateFormat = "EEEdataTaskWithURL(url, dd MMMM yyyy HH:mmcompletionHandler:ss zzzz"
{ data, respinse, error in
for var idict = 0; i < entriesNSJSONSerialization.count; i++ {
JSONObjectWithData(data, varoptions: entryNSJSONReadingOptions.MutableContainers, =error: entries[i]nil) as! NSMutableDictionaryNSDictionary
if var responseData = entrydict["url;responseData"] = newsUrlString
as? NSDictionary {
if var dateStrfeed = entryresponseData["publishedDate;feed"] as! String
? NSDictionary {
if var dateentries = formatter.dateFromString(dateStr)
entryfeed["date;entries"] = date
as? NSArray }{
var formatter = self.entries.addObjectsFromArrayNSDateFormatter(entries as[AnyObject])
self.entriesformatter.sortUsingComparator({locale object1,= object2NSLocale(localeIdentifier: in"en-US")
var date1formatter.dateFormat = object1["date"] as! NSDate
;EEE, dd varMMMM date2yyyy =HH:mm:ss object2["datezzzz"] as! NSDate
var order = date1.compare(date2);
iffor ordervar =i = NSComparisonResult0; i < entries.OrderedAscendingcount; i++ {
returnvar NSComparisonResult.OrderedAscendingentry = entries[i] as! NSMutableDictionary
}
else if order entry["url"] = newsUrlString
var dateStr = NSComparisonResult.OrderedAscending{entry["publishedDate"] as! String
returnvar NSComparisonResultdate = formatter.OrderedAscendingdateFromString(dateStr)
entry["date"] = date
}
self.entries.addObjectsFromArray(entries as[AnyObject])
self.entries.sortUsingComparator({ object1, object2 in
var date1 = object1["date"] as! NSDate
var date2 = object2["date"] as! NSDate
var order = date1.compare(date2)
if order == NSComparisonResult.OrderedAscending {
return NSComparisonResult.OrderedAscending
}
else if order == NSComparisonResult.OrderedAscending{
return NSComparisonResult.OrderedAscending
}
return order
})
}
}
}
}
dispatch_async(dispatch_get_main_queue(),{
self.tableView.reloadData()
dispatch_async(dispatch_get_main_queue(),{
self.tableView.reloadData()
})
})
})
task.resume()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarningviewDidLoad() {
super.didReceiveMemoryWarningviewDidLoad()
// Dispose of any resources that can be recreated.
}
override// funcDo tableView(tableView:any UITableView,additional numberOfRowsInSection
setup after loading the section:view, Int)typically ->from Inta {nib.
return entries.count
}
override func tableViewdidReceiveMemoryWarning(tableView: UITableView,) cellForRowAtIndexPath{
indexPath: NSIndexPath) -> UITableViewCell {super.didReceiveMemoryWarning()
var cell// =Dispose tableView.dequeueReusableCellWithIdentifier("news")of as!any UITableViewCell
resources that can be recreated.
}
var entryoverride =func entries[indexPath.row]tableView(tableView: as!UITableView, NSDictionarynumberOfRowsInSection
section: varInt) titleLabel-> =Int cell.viewWithTag(1){
as! UILabel
titleLabel return entries.textcount
= entry["title"] as? String}
var descriptionLabeloverride =func cell.viewWithTagtableView(2) as! UILabel
descriptionLabel.text = entry["contentSnippet"] as? String
var urlString = entry["url"] as! String
var index =tableView: find(newsUrlStringsUITableView, urlString)cellForRowAtIndexPath
var imageName = imageNames[index!]
varindexPath: imageNSIndexPath) =-> UIImage(named:UITableViewCell imageName){
var imageViewcell = celltableView.viewWithTagdequeueReusableCellWithIdentifier(4"news") as! UIImageViewUITableViewCell
imageView.image = image
return cell
}
override func tableView(tableView:
UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var performSegueWithIdentifier("detail",entry sender:= entries[indexPath.row] as! NSDictionary
var titleLabel = cell.viewWithTag(1) as! UILabel
} titleLabel.text = entry["title"] as? String
var descriptionLabel = cell.viewWithTag(2) as! UILabel
descriptionLabel.text = entry["contentSnippet"] as? String
var urlString = entry["url"] as! String
var index = find(newsUrlStrings, urlString)
var imageName = imageNames[index!]
var image = UIImage(named: imageName)
var imageView = cell.viewWithTag(4) as! UIImageView
imageView.image = image
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("detail", sender: entries[indexPath.row])
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if segue.identifier == "detail" {
var detailController = segue.destinationViewController as! DetailController
detailController.entry = sender as! NSDictionary
}
}
}
}