Swift 解析

这篇博客介绍了如何在Swift应用中使用Alamofire进行网络请求,并结合UITableView展示数据。首先,导入Alamofire库,然后在ViewController中设置导航栏右侧按钮。接着,注册自定义的UITableViewCell以便在表格中显示数据。最后,通过注册 nib 文件来加载单元格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import Alamofire
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

var dicc:NSDictionary?

var dic1:NSArray = []
var table:UITableView?

var dict:NSDictionary = [:]
var data:NSMutableDictionary = [:]
var dictr:NSDictionary = [:]
override func viewDidLoad() {
    super.viewDidLoad()
   
    
   setUI()
    
    resultData()
}
func setUI(){
    
    let SecBar:UISearchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: 41, height: 41))
    self.navigationItem.titleView = SecBar

// self.navigationItem.rightBarButtonItem UIBarButtonItem(barButtonSystemItem: “dasd”, target: self, action: .done, action:select(right))
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title:“daw”, style: UIBarButtonItem.Style.plain, target: self, action: #selector(self.right))

    //表格
    table = UITableView.init(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height), style: .grouped)
    table?.delegate = self
    table?.dataSource = self
    let viewTop:UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: 414, height: 200))
    viewTop.backgroundColor = UIColor.red
    
    let imgView:UIImageView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: 414, height: 200))
    imgView.image = UIImage(named: "Snip20181115_1")
    
    
    viewTop.addSubview(imgView)
    self.table?.tableHeaderView = viewTop
    
    
    
    self.view .addSubview(table!)

// self.table?.register(newTableViewCell.self, forCellReuseIdentifier: “cells”)
self.table?.register( UINib(nibName: “newTableViewCell”, bundle: nil), forCellReuseIdentifier: “cells”)

}
@objc func right(){
print("sda" )
}
func resultData(){
    Alamofire.request("http://live.ximalaya.com/live-web/v4/homepage?device=iPhone", method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
        if(response.error == nil){
            print("请求成功")
            //                print(response.result.value as Any)
            self.dicc = response.result.value as? NSDictionary
            
            self.dictr = self.dicc?.object(forKey: "data") as! NSDictionary
    
            self.dic1 = (self.dictr.object(forKey: "localRadios") as! NSArray)

// print(self.dic1 as Any)

            self.table?.reloadData()
            
        }else{
            print("请求失败\(String(describing: response.error))")
        }
        //解析网址
    }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dic1.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:newTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cells") as! newTableViewCell
    table?.rowHeight = 120
    let name:NSDictionary = dic1[indexPath.row] as! NSDictionary
    let md:BaseModel =  BaseModel()
    md.dic = name
    
    cell.setCellWithData(model: md)
    
    
    cell.accessoryType = UITableViewCell.AccessoryType.detailDisclosureButton
    return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    
    if section == 0 {
        return "ath.row] as! NSDictionary"
    }else{
        return "let name:NSDictionary  nary"
    }
}
func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 30.0
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值