iOS项目之Swift新闻App(三)—访问新闻详情
2015-12-12 12:39
441 查看
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~
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~
相关文章推荐
- Swift中的if语句使用where
- Swift -- 组合赋值运算符
- Swift注释风格
- Swift2.1-初始化
- Swift2.1-继承
- Swift应用开源项目推荐
- swift在github上开源的地址
- 初探swift语言的学习笔记九(OC与Swift混编)
- 关于OC与swift混编时-swift.h更新问题
- 关于swift,OC,C++混合编程(二)
- 关于swift,OC,C++混合编程(一)
- Swift 开源那点事儿
- Swift中编写单例的正确方式
- swift之mutating关键字
- swift分享到微信
- Swift:可选类型(optional)与构造函数
- The beginning iOS8 Programming with Swift 中文翻译 - 8
- swift NSURLSession 使用方法
- Swift 利用UIScrollView和两个UIImageView实现图片轮滚
- Swift 函数做参数和闭包做参数的一个细节差别