1.用户是通过点击TableView上的某一行来对新闻进行阅读,在程序中对应这TableView的
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath),通过实现该方法,来响应回调用户点击某一个TableView之后该做的操作。在这里是通过获得每一个新闻的id,来拼接成对应的url,然后对该url进行请求访问,代码如下:
/**
Description点击cell之后进行的相关操作
*/
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let newsController = NewsListViewController()
newsController.id = self.items1[indexPath.row].id
self.navigationController?.pushViewController(newsController, animated: true)
}
2.上述代码中通过导航栏的pushViewController进行跳转到下一个ViewController,方便返回。
class NewsListViewController: UIViewController {
var webView = UIWebView(frame: CGRectMake(0,0,UIScreen.mainScreen().bounds.width,UIScreen.mainScreen().bounds.height))
var listUrl = "http://daily.zhihu.com/story/"
var id:Int!
override func viewDidLoad() {
super.viewDidLoad()
//加载新闻详情
loadRequest(listUrl+String(self.id))
self.view.addSubview(webView)
}
func loadRequest(url: String){
self.webView.loadRequest(NSURLRequest(URL: NSURL(string: url)!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
3.具体代码请见我的Github ,欢迎star~