swift tabview 带参数请求网络。多条目展示。json解析,逃逸闭包

效果:

用到的第三方

# Uncomment the next line to define a global platform for your project
 platform :ios, '9.0'

target 'News' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for News
  pod 'Alamofire'
  pod 'SwiftyJSON'
  pod 'HandyJSON'

end

请求网络地址与参数

//
//  Const.swift
//  News
//
//  Created by liuan on 2020/10/7.
//

import UIKit

let screenWidth = UIScreen.main.bounds.width

let screentHeight =  UIScreen.main.bounds.height
//服务器请求
let BASE_URL = "https://is.snssdk.com"

let device_id:String = "6096495334"

let IID:String = "5034850950"

数据bean

//
//  MyCellModel.swift
//  News
//
//  Created by liuan on 2020/10/7.
//

import Foundation
import HandyJSON

struct MyCellModel:HandyJSON {
    var grey_text:String = ""
    var text:String = ""
    var url:String = ""
    var key:String = ""
    var tip_new:Int = 0
}


struct MyConcern:HandyJSON {
    var name:String?
    var url:String?
    var total_count:Int?
    var description:String?
    var time:String?
    var type:String?
    var icon:String?
    var userid:Int?
    var is_verify:Bool?
    var media_id:Int?
    var tips:Bool?
    var id:Int?
    var user_auth_info:String?
    var userAuthInfo: UserAuthInfo?{
        return UserAuthInfo.deserialize(from:user_auth_info)
    }
    
    
}

struct UserAuthInfo: HandyJSON {
    var auth_type:Int?
    var auth_info:String?
}

 

网络请求工具类,用到了逃逸闭包,返回请求参数后刷新了网络请求

//
//  NetworkToolProtocol.swift
//  News
//
//  Created by liuan on 2020/10/7.
//

import Foundation

import Alamofire
import SwiftyJSON
protocol NetworkoolProtocol {
    // -----------我的mine--------
    // 我的界面 cell的数据
    static func loadmyCellData(completioHandler: @escaping(_ sections:[[MyCellModel]])->())
    //我的关注数据
    static func loadMyConcern()
    
    
}

extension NetworkoolProtocol{
    
}


struct NetworkTool:NetworkoolProtocol {
    // -----------我的mine--------
    // 我的界面 cell的数据
    static func loadmyCellData(completioHandler: @escaping(_ sections:[[MyCellModel]])->()) {
        let url = BASE_URL + "/user/tab/tabs/?"
        let params = ["device_id":device_id]
        Alamofire.request(url,parameters: params).responseJSON{(response) in
            guard response.result.isSuccess else{
                //d网络错误提示信息
                return
                
    }
            
            if let value = response.result.value{
        
                //swiftyjson里面的
                let json = JSON(value)
                print(value)
                guard json["message"]=="success" else {
                    return
                }
                if let data = json["data"].dictionary{
                  
                    if let sections = data["sections"]?.array{
                        var sectionnArray = [[MyCellModel]]()
                        for item in sections {
                            var rows = [MyCellModel]()
                            for row in item.arrayObject! {
                                let myCellModel  = MyCellModel.deserialize(from: row as? NSDictionary)
                                rows.append(myCellModel!)
                            }
                            sectionnArray.append(rows)
                        }
                        completioHandler(sectionnArray)
                    }
                }
            }
        }
            
    }
    //我的关注数据
    static func loadMyConcern() {
        
    }
    
    
}

主要的tabView Controller

//
//  MineViewController.swift
//  News
//
//  Created by liuan on 2020/10/5.
//

import UIKit

class MineViewController: UITableViewController {
    var sections = [[MyCellModel]]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
        tableView.backgroundColor = UIColor.globalBackGroundColor()
       
        tableView.register( UINib(nibName: String(describing:MyOtherCell.self), bundle: nil), forCellReuseIdentifier: String(describing:MyOtherCell.self))
       //获取我的cell的数据
        NetworkTool.loadmyCellData{(sections) in
            let string = "{\"text\":\"我的关注\",\"grey_text\":\"\"}"
            let myConcern = MyCellModel.deserialize(from: string)
            var myConcerns = [MyCellModel]()
            myConcerns.append(myConcern!)
            self.sections.append(myConcerns)
            self.sections+=sections
            self.tableView.reloadData()
        }
    }
    



}

extension MineViewController{
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 10))
     
        view.backgroundColor = UIColor.globalBackGroundColor()
        return view
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MyOtherCell.self)) as! MyOtherCell
        let section = sections[indexPath.section]
        let myCellModel = section[indexPath.row]
        cell.leftLabel.text = myCellModel.text
        cell.rightLabel.text = myCellModel.grey_text
        return cell
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

UIColor 扩展

//
//  UiColorExtension.swift
//  geekTime
//
//  Created by liuan on 2020/9/14.
//  Copyright © 2020 liuan. All rights reserved.
//

import UIKit
extension UIColor {
    static func hexColor(_ hexValue:Int,alpahValue:Float)->UIColor{
        return UIColor(red: CGFloat((hexValue & 0xFF0000) >> 16 ) / 255, green: CGFloat((hexValue & 0x00FF00) >> 8) / 255, blue: CGFloat(hexValue & 0x0000FF ) / 255, alpha: CGFloat(alpahValue))
    }
    
    static func hexColor(_ hexValue:Int)->UIColor{
        return hexColor(hexValue,alpahValue: 1)
         
     }
 
    convenience init(_ hexValue:Int,alpahValue:Float) {
       
        self.init(red: CGFloat((hexValue & 0xFF0000) >> 16 ) / 255, green: CGFloat((hexValue & 0x00FF00) >> 8) / 255, blue: CGFloat(hexValue & 0x0000FF ) / 255, alpha: CGFloat(alpahValue))
    }
    
    convenience init(_ hexValue:Int) {
        self.init(hexValue,alpahValue:1)
    }
    
    convenience init(r:CGFloat,g:CGFloat,b:CGFloat,alpha:CGFloat = 1.0){
        self.init(displayP3Red: r/255.0, green: g/255.0, blue: b/255.0, alpha: alpha)
    }
    
    class func globalBackGroundColor() -> UIColor{
        return UIColor(r: 248, g: 249, b: 247)
    }
}

用到了 MyOtherCell +xlib

//
//  MyOtherCell.swift
//  News
//
//  Created by liuan on 2020/10/7.
//

import UIKit

class MyOtherCell: UITableViewCell {

    @IBOutlet weak var leftLabel: UILabel!
    
    @IBOutlet weak var rightLabel: UILabel!
    
    @IBOutlet weak var rightImageView: UIImageView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

xlib 不知道代码如何复制

就是这个样子的

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值