智能文章系统实战-iOS文章客户端(9)
发布于:2018-6-21 22:24 作者:admin 浏览:29341. 文章实体类
class Item{
var title="";
var url=""
}
2.列表程序
import UIKit
class ListViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var dataSource:[Item]=[]
var tv:UITableView?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor=UIColor.white
//列表
tv=UITableView(frame:CGRect(x: 0, y: 20, width: self.view.frame.size.width, height: self.view.frame.size.height-50))
tv!.register(UITableViewCell.self, forCellReuseIdentifier: "NewsCell")
tv!.delegate=self;
tv!.dataSource=self;
self.view.addSubview(tv!)
self.loadJsonData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell=tv!.dequeueReusableCell(withIdentifier: "NewsCell", for: indexPath)
cell.accessoryType=UITableViewCellAccessoryType.disclosureIndicator
cell.textLabel?.text=dataSource[indexPath.row].title
return cell;
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tv!.deselectRow(at: indexPath, animated: true)
let itemTV=ItemViewController()
itemTV.tit=self.dataSource[indexPath.row].title
itemTV.url=self.dataSource[indexPath.row].url
self.present(itemTV, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func loadJsonData()
{
let url="http://news.demo.com/app.php"
let nsurl = URL(string:url)
let request = NSMutableURLRequest.init(url:nsurl!)
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if (error == nil) {
do{
let responseData:NSDictionary = try JSONSerialization.jsonObject(with:data!, options:JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary
let jsonArr=responseData["data"] as! [[String: Any]];
for json in jsonArr {
let N1=Item()
N1.title=json["title"] as! String
N1.url="http://news.demo.com/h5.php?action=show&id=\(json["id"] as! String)"
self.dataSource.append(N1)
}
DispatchQueue.main.async{
self.tv!.reloadData();
}
}catch{
}
}
}
dataTask.resume()
}
}
3.详细页程序
import UIKit
class ItemViewController: UIViewController {
var tit:String="";
var url:String="";
var webview:UIWebView?
override func viewDidLoad() {
super.viewDidLoad()
//关闭按钮
let btn=UIButton(frame: CGRect(x: 0, y: 20, width: self.view.bounds.size.width, height: 20))
btn.setTitle("返回", for: .normal)
btn.setTitleColor(UIColor.red, for: UIControlState.normal)
btn.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
self.view.addSubview(btn)
//WEB浏览器
self.view.backgroundColor=UIColor.white
let webview=UIWebView(frame: self.view.bounds)
webview.frame.origin.y=45;
let urlobj = URL(string:self.url)
let request = URLRequest(url:urlobj!)
webview.loadRequest(request);
self.view.addSubview(webview)
}
func btnClick(_ sender:UIButton)
{
self.dismiss(animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
