swift json

在这里插入图片描述

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    //底部标签控制器
    let oneNav = UINavigationController(rootViewController: OneVC())
    oneNav.tabBarItem = UITabBarItem(title: "首页", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
    
    let twoNav = UINavigationController(rootViewController: VCTwo())
    twoNav.tabBarItem = UITabBarItem(title: "微淘", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
    
    let threeNav = UINavigationController(rootViewController: ThreeVC())
    threeNav.tabBarItem = UITabBarItem(title: "消息", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
    
    let fourNav = UINavigationController(rootViewController: FourVC())
    fourNav.tabBarItem = UITabBarItem(title: "购物车", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
    let fiveNav = UINavigationController(rootViewController: FiveVC())
    fiveNav.tabBarItem = UITabBarItem(title: "我的", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
    
    let tbv = UITabBarController()
    tbv.viewControllers = [oneNav,twoNav,threeNav,fourNav,fiveNav]
    tbv.selectedViewController = twoNav
    window?.rootViewController = tbv
    
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}

import UIKit

class VCTwo: UIViewController {

private lazy var pageTitleView: MFPageTitleView = {
    let config = MFPageTitleViewConfig()
    config.titleColor = colorWithRGB(r: 43, g: 43, b: 43)
    config.titleSelectedColor = colorWithRGB(r: 211, g: 0, b: 0)
    config.titleFont = UIFont.systemFont(ofSize: 14, weight: .regular)
    config.indicatorColor = colorWithRGB(r: 211, g: 0, b: 0)
    let pageTitleView = MFPageTitleView(frame: CGRect(x: 0, y: navHeight, width: SCREEN_WIDTH, height: 41), titles: ["全部", "上新", "视频直播", "特别关注", "达人"], config: config)
    pageTitleView.pageTitleViewDelegate = self
    return pageTitleView
}()

private lazy var pageContentView: MFPageContentView = {
    var childControllers = [UIViewController]()
    for k in 0..<5 {
        var vc = UIViewController()
        let red: CGFloat = CGFloat(arc4random() % 256)
        let green: CGFloat = CGFloat(arc4random() % 256)
        let blue: CGFloat = CGFloat(arc4random() % 256)
        vc.view.backgroundColor = UIColor(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: 1.0)
        if k==4{
            vc = WeiTaoVC()
        }
        childControllers.append(vc)
    }
    
    let pageContentViewY = pageTitleView.frame.maxY
    let pageContentView = MFPageContentView(frame: CGRect(x: 0, y: pageContentViewY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-pageContentViewY), parentVC: self, childVCs: childControllers)
    pageContentView.pageContentViewDelegate = self
    return pageContentView
}()

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.white
    
    view.addSubview(pageTitleView)
    view.addSubview(pageContentView)

}

}

extension VCTwo: MFPageTitleViewDelegate, MFPageContentViewDelegate {
func selectedIndexInPageTitleView(pageTitleView: MFPageTitleView, selectedIndex: Int) {
self.pageContentView.setPageContentViewCurrentIndex(currentIndex: selectedIndex)
}
func pageContentViewScroll(progress: CGFloat, originalIndex: Int, targetIndex: Int) {
self.pageTitleView.setPageTitleView(progress: progress, originalIndex: originalIndex, targetIndex: targetIndex)
}
}

import UIKit

class WeiTaoVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

let mArr:NSMutableArray = []
var cellHight:CGFloat = 0
var tv:UITableView?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    
     NotificationCenter.default.addObserver(self, selector: #selector(test), name: NSNotification.Name(rawValue:"cellHight"), object: nil)
    
    view.backgroundColor = UIColor.white
    
    let path = Bundle.main.path(forResource: "daren", ofType: "json")
    let url = URL(fileURLWithPath: path!)
    
    do {
        let data = try Data(contentsOf: url)
        let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
        // print(jsonData)
        let jsonArr:NSArray = jsonData as! NSArray
        print(jsonArr)
        
        for item in jsonArr {
            
            let k:NSDictionary = item as! NSDictionary
            
            if k.object(forKey: "type") as! String == "0"{
                
                let md:OneMD = OneMD(headImg: k.object(forKey: "head") as! String, title: k.object(forKey: "title") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! String, time: k.object(forKey: "time") as! String, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
                mArr.add(md)
                
            }else if k.object(forKey: "type") as! String == "1" {
                
                let md:TwoMD = TwoMD(headImg: k.object(forKey: "head") as! String, title: k.object(forKey: "title") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! NSMutableArray, time: k.object(forKey: "time") as! String, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
                mArr.add(md)
            }else{
                let md:ThreeMD = ThreeMD(headImg: k.object(forKey: "head") as! String, title: k.object(forKey: "title") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! String, time: k.object(forKey: "time") as! String, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
                mArr.add(md)
            }
            
        }
        
        
        
    } catch let error as Error? {
        print(error as Any)
    }
    
    
    tv = UITableView(frame:CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height-120), style: .plain)
    view.addSubview(tv!)
    
    tv!.delegate = self
    tv!.dataSource = self
    
    tv!.register(UINib(nibName: "OneCell", bundle: Bundle.main), forCellReuseIdentifier: "cell1")
    tv?.register(UINib(nibName: "TwoCell", bundle: Bundle.main), forCellReuseIdentifier: "cell2")
    tv!.register(UINib(nibName: "ThreeCell", bundle: Bundle.main), forCellReuseIdentifier: "cell3")
    
    tv!.tableFooterView = UIView()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return mArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let item = mArr[indexPath.row]
    
    if item is OneMD{
        let md:OneMD = mArr[indexPath.row] as! OneMD
        
        let cell:OneCell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! OneCell
        cell.setCellWithData(model: md)
        return cell
    } else if item is TwoMD{
        let md:TwoMD = mArr[indexPath.row] as! TwoMD
        
        let cell:TwoCell = tableView.dequeueReusableCell(withIdentifier: "cell2") as! TwoCell
        cell.setCellWithData(model: md)
        print(cellHight)
        return cell
    }else{
        let md:ThreeMD = mArr[indexPath.row] as! ThreeMD
        
        let cell:ThreeCell = tableView.dequeueReusableCell(withIdentifier: "cell3") as! ThreeCell
        cell.setCellWithData(model: md)
        return cell
    }
    
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    let item = mArr[indexPath.row]
    
    if item is TwoMD{
         return cellHight+10
        
    } else{
        
        return 200
    }
}

@objc func test(nofi : Notification){
    
    cellHight = nofi.userInfo!["post"] as! CGFloat
    tv?.reloadData()
    /// 移除通知
    NotificationCenter.default.removeObserver(self)
}

}

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    let path = Bundle.main.path(forResource: "daren", ofType: "json")
    let url = URL(fileURLWithPath: path!)
    
    do {
          let data = try Data(contentsOf: url)
          let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
         // print(jsonData)
          let jsonDic:NSArray = jsonData as! NSArray
          print(jsonDic)
        
        
        
        
    } catch let error as Error? {
          print(error as Any)
    }

}

}

猜你喜欢

转载自blog.csdn.net/qq_43656530/article/details/86561452
今日推荐