美文网首页
swift喜马拉雅123

swift喜马拉雅123

作者: 970517 | 来源:发表于2018-09-21 18:55 被阅读0次

自动集成

pod 'Alamofire'
pod 'SQAutoScrollView'
pod 'SwiftyJSON'

Model属性

var title = ""
var create_time = ""
var img = ""
var author = ""

ViewController导入头文件

import Alamofire
import SQAutoScrollView
import SwiftyJSON

全局

var dataArray:[Model] = []
var table : UITableView?

解析

  let goods1 = Model()
    goods1.title = "苏打粉谷咖啡机不可失打击报复恶补的斯洛伐克你的书法家"
    goods1.img = "1"
    goods1.create_time = "9月20日"     
    let goods2 = Model()
    goods2.title = "先创建背阔肌烤焦了获得过酷狗宽高一唠嗑呢我我也很不给力电饭煲内"
    goods2.img = "2"
    goods2.create_time = "9月20日"
    let goods3 = Model()
    goods3.title = "水电费in卡的会计师被福建日报打死哦发货IE儿童版怒江私对公"
    goods3.img = "3"
    goods3.create_time = "9月20日"
    dataArray = [goods1,goods2,goods3]

方法

    self.view.addSubview(pageTitleView)
    loadCycleView()
    AnalyticalData()
    Table()

//标题滚动

private lazy var pageTitleView: MFPageTitleView = {
    let config = MFPageTitleViewConfig()
    let pageTitleView = MFPageTitleView(frame: CGRect(x: 0, y: navHeight, width: SCREEN_WIDTH, height: 41), titles: ["推荐","快讯","深度","实时对话","行情分析"], config: config)
    pageTitleView.pageTitleViewDelegate = self as? MFPageTitleViewDelegate
    return pageTitleView
}()

//MARK:解析数据,添加轮播图

fileprivate func loadCycleView() {       
    //Alamofire解析数据
    let parameters = ["method":"app.resource.getimg","position":"News-Banner"]
    Alamofire.request("http://test.bit.api.meeboo.cc", method: .get, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
        if(response.error == nil){
            print(response.result.value)            
        }else{
            print("请求失败\(String(describing: response.error))")              
        }         
    }

图片解析
//解析出的图片网址字符串

    let urls = ["http://www.zhanglirong.cn/themes/simplebootx_en-us/Public/assets/images/demo/1.jpg",                    "http://www.zhanglirong.cn/themes/simplebootx_en-us/Public/assets/images/demo/3.jpg",                    "http://www.zhanglirong.cn/themes/simplebootx_en-us/Public/assets/images/demo/2.jpg",
                ]        
    let cycleView = SQAutoScrollView(frame: CGRect.init(x: 0, y: 105, width: view.bounds.size.width, height: 150), urls: urls, didItemCallBack: { (view, index) in
        print("view--->\(view), index-->\(index)")
    })
    view.addSubview(cycleView)

}

自定义表格

func Table()  {
    table = UITableView(frame: CGRect(x: 0, y: 260, width: self.view.bounds.size.width, height: self.view.bounds.size.height - 260), style: .plain)
    table?.backgroundColor = UIColor.cyan     table?.register(NSClassFromString("CustomTableViewCell"), forCellReuseIdentifier: "CustomCell")
    table?.delegate = self
    table?.dataSource = self
    self.view.addSubview(table!)

}

表格方法

func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
}else{
return 3
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = NewsTableViewCell(style: .default, reuseIdentifier: "CustomCell")
if indexPath.section == 0 {
let img = UIImageView(frame: CGRect(x: 10, y: 10, width: 50, height: 40))
img.image = UIImage(named: "4")
cell.addSubview(img)
let lab = UILabel(frame: CGRect(x: 150, y: 10, width: 300, height: 70))
lab.numberOfLines = 0
lab.text = "成本价可不擦李四;发送到就肯定会擦本身就看不出坚实"
cell.addSubview(lab)
let lab1 = UILabel(frame: CGRect(x: 300, y: 50, width: 10, height: 10))
lab1.text = ">"
cell.addSubview(lab1)
}else{
cell.setValueForCell(model: dataArray[indexPath.row])
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 130
}

解析表格数据

func AnalyticalData() {
//MARK:解析网络数据
let parameters = ["method":"app.news.getarticlelist","class_id":"1"]
Alamofire.request("http://test.bit.api.meeboo.cc", method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
if(response.error == nil){
print(response.result.value!)
let json = JSON(response.value ?? "")
for item in json["data"]["list"].arrayValue{
let model = Model()
model.title = item["title"].stringValue
model.create_time = item["create_time"].stringValue
model.author = item["author"].stringValue
model.img = item["img"].stringValue
self.dataArray.append(model)
}
OperationQueue.main.addOperation {
self.table?.reloadData()
}
}else{
print("请求失败(String(describing: response.error))")
}
}
}

自定义表格里内容

var title : UILabel?
var ImgV : UIImageView?
var create_time : UILabel?
var author : UILabel?    
var imgArr = ["1","2","3","4"]       
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}    
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    lauouUI()
}    
func lauouUI() {        
    ImgV = UIImageView(frame: CGRect(x: 10, y: 15, width: 110, height: 90))
    ImgV?.backgroundColor = .red
    self.addSubview(ImgV!)        
    title = UILabel(frame: CGRect(x: 130, y: 5, width: 300, height: 70))
    title?.numberOfLines = 0
    self.addSubview(title!)  
    create_time = UILabel(frame: CGRect(x: 350, y: 100, width: 60, height: 20))
    create_time?.backgroundColor = .yellow
    create_time?.font = UIFont.systemFont(ofSize: 12)
    self.addSubview(create_time!)    
}
func setValueForCell(model:Model)  {    
    self.title?.text = model.title
    self.create_time?.text = model.create_time
    self.ImgV?.image = UIImage(named: model.img)
}

相关文章

网友评论

      本文标题:swift喜马拉雅123

      本文链接:https://www.haomeiwen.com/subject/lveznftx.html