class NewsViewController: UITableViewController, UISearchResultsUpdating {
var rssItems : [(title: String, description : String, pubDate : String, link : String)]?
var filteredRssItems = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.separatorColor = UIColor.clearColor()
self.view.backgroundColor = UIColor(colorLiteralRed: 1.4, green: 1.4, blue: 1.4, alpha: 1)
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.resultSearchController.searchBar
self.tableView.reloadData()
}
override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated)
let feedParser = FeedParser()
feedParser.parseFeed("http://rss.etnews.co.kr/Section902.xml", completionHandler: { (rssItems: [(title: String, description: String, pubDate: String, link: String)]) -> Void in
self.rssItems = rssItems
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .None)
})
})
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let rssItems = rssItems else {
return 0
}
if (self.resultSearchController.active) {
return self.filteredRssItems.count
} else {
return rssItems.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewsTableViewCell
if (self.resultSearchController.active) {
cell.titleLabel.text = filteredRssItems[indexPath.row]
cell.descriptionLabel.text = filteredRssItems[indexPath.row]
cell.dateLabel.text = filteredRssItems[indexPath.row]
} else {
cell.titleLabel.text = rssItems![indexPath.row].title
cell.descriptionLabel.text = rssItems![indexPath.row].description
cell.dateLabel.text = rssItems![indexPath.row].pubDate
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let item = rssItems?[indexPath.row]
if let url = NSURL(string: item!.link) {
let safariController = SFSafariViewController(URL: url, entersReaderIfAvailable: true)
presentViewController(safariController, animated: true, completion: nil)
}
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 190.0
}
@IBAction func shartBtnTapped(sender: AnyObject) {
let actionSheet = UIAlertController(title: "", message: "Share your Note", preferredStyle: UIAlertControllerStyle.ActionSheet)
// Configure a new action for sharing the note in Twitter.
let tweetAction = UIAlertAction(title: "Share on Twitter", style: UIAlertActionStyle.Default) { (action) -> Void in
}
// Configure a new action to share on Facebook.
let facebookPostAction = UIAlertAction(title: "Share on Facebook", style: UIAlertActionStyle.Default) { (action) -> Void in
}
// Configure a new action to show the UIActivityViewController
let moreAction = UIAlertAction(title: "More", style: UIAlertActionStyle.Default) { (action) -> Void in
let activityViewController = UIActivityViewController(activityItems: [self.title!], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypeMail]
self.presentViewController(activityViewController, animated: true, completion: nil)
}
let dismissAction = UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel) { (action) -> Void in
}
actionSheet.addAction(tweetAction)
actionSheet.addAction(facebookPostAction)
actionSheet.addAction(moreAction)
actionSheet.addAction(dismissAction)
presentViewController(actionSheet, animated: true, completion: nil)
}
func showAlertMessage(message: String!) {
let alertController = UIAlertController(title: "EasyShare", message: message, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default, handler: nil))
presentViewController(alertController, animated: true, completion: nil)
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
self.filteredRssItems.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (self.rssItems as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.filteredRssItems = array as! [String]
self.tableView.reloadData()
}
@IBAction func logoutBtnTapped(sender: AnyObject) {
NSUserDefaults.standardUserDefaults().setValue(nil, forKey: "uid")
self.dismissViewControllerAnimated(true, completion: nil)
}
please help me~ i have a problem. let array = (self.rssItems as NSArray).filteredArrayUsingPredicate(searchPredicate) => Cannot convert value of type “[(title: String, description : String, pubDate : String, link : String)]?” to type 'NSArray' in coercion
please advice me solution~