zl程序教程

您现在的位置是:首页 >  后端

当前栏目

swift 列表

列表 swift
2023-09-27 14:27:38 时间

效果

构造

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

import Foundation

struct Product {
    var name:String
    var price: Int
    var imageUrl:String
    var desc:String
    var teacher:String
    var total:Int
    var update:Int
    var detail:String
    var curseList:String
    var studentCount:Int
}

假数据

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

import Foundation
class FakeData {
    private static  var bannerList = [String]()
    private static var products = [Product]()
    private static var deals=[Deal]()
    
    static func createBanners()->[String]{
        if bannerList.count == 0 {
            bannerList = [
                "https://via.placeholder.com/450x150",
                "https://via.placeholder.com/450x151",
                "https://via.placeholder.com/450x152"
            ]
        }
        return bannerList
    }
    
    static func createProducts()->[Product]{
        if products.count == 0 {
            products = [
                Product(name: "项目实战20讲", price: 1, imageUrl: "https://via.placeholder.com/254x336", desc: "当下,项目杀手狂妃卡欢乐颂发货啦好看货收到了粉红色的两幅画打发链接", teacher: "刘安", total: 20, update: 9, detail: "dasdjaskldjasldjasldsadlk", curseList: "开篇词(1)", studentCount: 234234),
                Product(name: "项目实战18讲", price: 2, imageUrl: "https://via.placeholder.com/254x336", desc: "当下,项目杀手狂妃卡欢乐颂发货啦好看货收到了粉红色的两幅画打发链接", teacher: "刘安", total: 20, update: 9, detail: "dasdjaskldjasldjasldsadlk", curseList: "开篇词(1)", studentCount: 234234),
                Product(name: "项目实战19讲", price: 3, imageUrl: "https://via.placeholder.com/254x336", desc: "当下,项目杀手狂妃卡欢乐颂发货啦好看货收到了粉红色的两幅画打发链接", teacher: "刘安", total: 20, update: 9, detail: "dasdjaskldjasldjasldsadlk", curseList: "开篇词(1)", studentCount: 234234),
                Product(name: "项目实战21讲", price: 4, imageUrl: "https://via.placeholder.com/254x336", desc: "当下,项目杀手狂妃卡欢乐颂发货啦好看货收到了粉红色的两幅画打发链接", teacher: "刘安", total: 20, update: 9, detail: "dasdjaskldjasldjasldsadlk", curseList: "开篇词(1)", studentCount: 234234)
            ]
        }
        return products
    }
    
    
    static func createsDeals() -> [Deal]{
        if deals.count == 0 {
            deals = FakeData.createProducts().map {
                Deal(product: $0, process: 1)
            }
            
        }
        return deals
        
        
    }}

单独把列表拎出来

//
//  ProductList.swift
//  geekTime
//
//  Created by liuan on 2020/9/15.
//  Copyright © 2020 liuan. All rights reserved.
//

import Foundation
import UIKit
import Kingfisher
class ProductCell: UITableViewCell {
    let priceLabel:UILabel
    let productImageView: UIImageView
    
    var item:Product?{
        didSet{
            if let  p = self.item{
                self.productImageView.kf.setImage(with: URL(string: p.imageUrl))
                self.textLabel?.text = p.name
                self.detailTextLabel?.text = p.desc
                self.priceLabel.text = "¥\(p.price)"
            }
        }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        priceLabel = UILabel(frame: .zero)
        productImageView = UIImageView()
        super.init(style:style,reuseIdentifier:reuseIdentifier)
        self.steupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func steupViews() {
        textLabel?.textColor = UIColor.hexColor(0x333333)
        detailTextLabel?.textColor = UIColor.hexColor(0x999999)
        detailTextLabel?.numberOfLines = 2
        priceLabel.textColor = UIColor.hexColor(0xe23b41)
        priceLabel.font = UIFont.systemFont(ofSize:15)
        productImageView.contentMode = .scaleAspectFit
        productImageView.clipsToBounds = true
        
        contentView.addSubview(priceLabel)
        contentView.addSubview(productImageView)
        
        productImageView.snp.makeConstraints({ make in
            make.left.equalTo(contentView).offset(20)
            make.top.equalTo(contentView).offset(10)
            make.width.equalTo(80)
            make.height.equalTo(100)
        })
        
        textLabel?.snp.makeConstraints({make in
            make.left.equalTo(productImageView.snp_right).offset(12)
            make.top.equalTo(productImageView)
            make.right.equalTo(contentView).offset(-20)
        })
        
        priceLabel.snp.makeConstraints({make in
            make.left.equalTo(textLabel!)
            make.centerY.equalTo(contentView)
        })
        detailTextLabel?.snp.makeConstraints({make in
            make.left.equalTo(textLabel!)
            make.bottom.equalTo(productImageView)
            make.right.equalTo(contentView).offset(-20)
            
            }
        )
    }
}

class ProductList: UIView,UITableViewDataSource,UITableViewDelegate {
    var tableView:UITableView
    var items:[Product] = []{
        didSet{
            self.tableView.reloadData()
        }
    }
    override init(frame: CGRect) {
          tableView = UITableView(frame: .zero,style: .plain)
              super.init(frame: frame)
        self.setupViews()
    }
  
    func setupViews(){
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView()
        self.addSubview(tableView)
        tableView.snp.makeConstraints({make in
            make.edges.equalToSuperview()
            })
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as? ProductCell
        if cell == nil {
            cell = ProductCell(style: .subtitle, reuseIdentifier:  "cellID")
        }
        cell?.item = items[indexPath.row]
        return cell!
    }
    
    //高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120
    }
    
    
        
}

使用

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

import UIKit
import Kingfisher
class HomeViewController: BaseViewController,BannerViewDataSource {
    func numberOfBanner(_ bannerView: BannerView) -> Int {
        return FakeData.createBanners().count
    }
    
    func viewForBanner(_ bannerView: BannerView, index: Int, convertView: UIView?) -> UIView {
        if let view = convertView as? UIImageView{
            view.kf.setImage(with: URL(string: FakeData.createBanners()[index]))
            return view
        }else {
            let imageView = UIImageView()
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
            imageView.kf.setImage(with: URL(string:  FakeData.createBanners()[index]))
            return imageView
        }
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let bannerView = BannerView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 176))
        bannerView.autoScrollInterval = 2
        bannerView.isInfinite = true
        bannerView.dataSource = self
     
        view.addSubview(bannerView)
        
        let productList = ProductList()
        productList.items = FakeData.createProducts()
        view.addSubview(productList)
        productList.snp.makeConstraints({(make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(bannerView.snp_bottom).offset(5)
        } )
        
    }
    



}

填充

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

import UIKit
import Kingfisher
class HomeViewController: BaseViewController,BannerViewDataSource {
    func numberOfBanner(_ bannerView: BannerView) -> Int {
        return FakeData.createBanners().count
    }
    
    func viewForBanner(_ bannerView: BannerView, index: Int, convertView: UIView?) -> UIView {
        if let view = convertView as? UIImageView{
            view.kf.setImage(with: URL(string: FakeData.createBanners()[index]))
            return view
        }else {
            let imageView = UIImageView()
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
            imageView.kf.setImage(with: URL(string:  FakeData.createBanners()[index]))
            return imageView
        }
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let bannerView = BannerView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 176))
        bannerView.autoScrollInterval = 2
        bannerView.isInfinite = true
        bannerView.dataSource = self
     
        view.addSubview(bannerView)
        
        let productList = ProductList()
        productList.items = FakeData.createProducts()
        view.addSubview(productList)
        productList.snp.makeConstraints({(make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(bannerView.snp_bottom).offset(5)
        } )
        
    }
    



}