IOS开发简易的网易新闻页面

IOS开发简易的网易新闻页面

这是简易的网易新闻,使用swift语法去写的
IOS开发简易的网易新闻页面

git 仓库地址:
https://e.coding.net/lujun1/wangyijianyixinwenswiftbanben/News1.git

//
//  NetworkTools.swift
//  News
//
//  Created by lujun on 2021/10/3.
//

import UIKit
import Alamofire

enum MethodType {
    case GET
    case POST
}
class NetworkTools{
    //类方法
    class func requestData(type: MethodType,urlString: String, parameters: [String : Any]? = nil,finishedCallback : @escaping ( _ results : Any) -> ()){
//        let type1 = type==MethodType.GET ? ".get" : ".post"
        if(type==MethodType.GET){
            Alamofire.AF.request(urlString).responseJSON { (response) in
                switch response.result {
                case .success(let json):
                    finishedCallback(json)
                    break
                case .failure(let error):
                    print("error:\(error)")
                    break
                }
            }
        }
        if(type==MethodType.POST){
            Alamofire.AF.request(urlString, method: .post, parameters: parameters).responseJSON { (response) in
                switch response.result {
                case .success(let json):
                    finishedCallback(json)
                    break
                case .failure(let error):
                    print("error:\(error)")
                    break
                }
            }
        }
    }
    //类方法
    class func requestDataWithParam(type: MethodType,urlString: String, parameters: [String : Any],finishedCallback : @escaping ( _ results : Any) -> ()){
        AF.request(urlString, parameters: parameters).responseJSON { (response) in
            switch response.result {
            case .success(let json):
                finishedCallback(json)
                break
            case .failure(let error):
                print("error:\(error)")
                break
            }
        }
    }
}


import UIKit

class NewsModel: NSObject {
   @objc var title: String?
//    var title: String = ""
    @objc var replyCount: Int = 0
    @objc var source: String?
    @objc var imgsrc: String?
    //有参构造 KVC
    init(dict: [String : Any]){
        super.init()
        setValuesForKeys(dict)
    }
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
    }
}
//
//  ViewController.swift
//  News1
//
//  Created by lujun on 2021/10/3.
//

import UIKit
private let kCellID: String = "kCellID"
class ViewController: UIViewController {
    private lazy var newsModel: [NewsModel] = [NewsModel]()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.frame = self.view.bounds
        tableView.dataSource = self
        tableView.register(UINib(nibName: "NewsViewCell", bundle: nil), forCellReuseIdentifier: kCellID)
        tableView.rowHeight = 90
//        tableView.register(UITableViewCell.self, forCellReuseIdentifier: kCellID)
        return tableView
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
         setupNavigationBar()
         loadData()
        self.view.addSubview(tableView)
    }

}
extension ViewController {
    private func setupNavigationBar (){
        navigationController?.navigationBar.backgroundColor = UIColor.red
        navigationItem.titleView = UIImageView(image: UIImage(named: "navigation_logo"))
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "navigation_search"), style: .plain, target: self, action: #selector(click))
    }
    @objc  private func click(){
        print("---")
    }
}
extension ViewController {
    private func loadData(){
        let data = NSDate()
        let d = data.timeIntervalSince1970
        //1633316114
        //1348649079062
        print("\(d)")
        NetworkTools.requestData(type: .GET, urlString: "http://c.m.163.com/nc/article/list/T1348649079062/0-20.html", parameters: nil) { results in
//            print(results)
//            let b = (results as AnyObject).write(toFile:"/Users/lujun/Desktop/lujun.plist",atomically: true)
//            print(b)
            guard let resultDict = results as? [String : Any] else {return}
            guard let dataArray = resultDict["T1348649079062"] as?[[String : Any]] else{ return }
            for dict in dataArray {
                let newMdoel = NewsModel(dict: dict)
                self.newsModel.append(newMdoel)
            }
//            let news1: NewsModel = self.newsModel.first!
//            print(news1.title!)
            self.tableView.reloadData()
        }
    }
}
extension ViewController : UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return newsModel.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kCellID, for: indexPath) as! NewsViewCell
        cell.newsModel = newsModel[indexPath.row]
        //        cell.textLabel?.text = "\(indexPath.row)"
        return cell
        
    }
}

import UIKit
import Kingfisher
class NewsViewCell: UITableViewCell {
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var replyCountLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var iconImageView: UIImageView!
    var newsModel: NewsModel?{
        didSet{ //监听属性方法
            titleLabel.text = newsModel?.title
            replyCountLabel.text = "\(newsModel?.replyCount ?? 0)在线"
            sourceLabel.text = newsModel?.source
            let url = URL(string: newsModel?.imgsrc ?? "")
            iconImageView.kf.setImage(with: url, placeholder: UIImage(named: "launchScreen_bottom_icon_wc"), options: nil, completionHandler: nil)
//            iconImageView.layer.cornerRadius = 10
            iconImageView.layer.masksToBounds = true
        }
    }
}
上一篇:快速滑动时 `cellForRow` 的调用次数


下一篇:iOS Self-Sizing优化