iOS项目之Swift新闻App(三)—访问新闻详情

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~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值