类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()
}
覆盖func viewDidAppear(animated:Bool){ super.viewDidAppear(动画)
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)
}
请帮助我〜我有一个问题。
let array =(self.rssItems as NSArray).filteredArrayUsingPredicate(searchPredicate)=>无法转换类型“[(title:String,description:String,pubDate:String,link:String)]的值”?“在强制中键入'NSArray'
请告诉我解决方案〜
答案 0 :(得分:0)
NSArray
只能包含符合AnyObject
的类型。元组不是AnyObject
,因此无法存储在NSArray
中。
但是,我看到您将过滤器的结果转换为[String]
。你是否意味着在应用过滤器之前提取元组中的一个条目?例如,像:
let array = (self.rssItems.map({ $0.description }) as NSArray).filteredArrayUsingPredicate(searchPredicate)
如果没有,您可能需要考虑替代filteredArrayUsingPredicate
。请考虑使用Swift数组和filter
使用您自己的过滤功能。