ios 将项目从一个数组移动到另一个数组

42fyovps  于 2023-03-20  发布在  iOS
关注(0)|答案(1)|浏览(159)

因此,我有很多麻烦,试图将一个项目从一个数组内移动到另一个数组,我有一个类,它包含2个数组,一个用于购物车页面,一个用于愿望清单,我希望能够在2个数组之间自由移动这些项目。

import UIKit

final class SneakersTableCell: UITableViewCell {
    private let productImageView = UIImageView()
    private let categoryLabel = UILabel()
    private let nameLabel = UILabel()
    private let priceLabel = UILabel()
    private let sizeLabel = UILabel()
    private let colorLabel = UILabel()
    private let removeButton = UIButton()
    private let clearImage = UIImage(named: "Image-1")
    let qtyButton = UIButton(type: .system)
    private let arrowQty = UIImage(named: "Arrow_Qty")?.withRenderingMode(.alwaysTemplate)
    private let wishButton = UIButton()
    let orderSummaryCell = OrderSummaryCell()

    var removeMe: ((SneakersTableCell) -> ())?
    
    var viewModel = CustomViewModel()

    override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
        super.init(style: .default,reuseIdentifier: "SneakersTableCell")
        print("New cell has been create")
        setupUI()
        setupConstraints()
        removeButton.addTarget(self, action: #selector(removeButtonTapped(_:)), for: .touchUpInside)
        
    }
    
    @objc func removeButtonTapped(_ sender: UIButton) {
        print("Tapped remove button")
        // call the closure
        removeMe?(self)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configure(product: BagProduct) {
        productImageView.image = product.image
        categoryLabel.text = product.category
        nameLabel.text = product.name
        priceLabel.text = "\(product.price)€"
        sizeLabel.text = "Size: \(product.size)"
        colorLabel.text = "Color: \(product.color)"
    }
}

前面的代码显示了wishButton在哪里。

import UIKit

protocol BagProduct {
    var image: UIImage { get }
    var category: String { get }
    var name: String { get }
    var price: Double { get }
    var size: Int { get }
    var color: String { get }
}
protocol WishProduct {
    var image: UIImage { get }
    var category: String { get }
    var name: String { get }
    var price: Double { get }
}

struct WishProducts: WishProduct {
    let image: UIImage
    let category: String
    let name: String
    let price: Double
    
    init(image: UIImage, category: String, name: String, price: Double) {
        self.image = image
        self.category = category
        self.name = name
        self.price = price
    }
}
    
struct BagProducts: BagProduct {
    let image: UIImage
    let category: String
    let name: String
    let price: Double
    let size: Int
    let color: String
    
    init(image: UIImage, category: String, name: String, price: Double, size: Int, color: String) {
        self.image = image
        self.category = category
        self.name = name
        self.price = price
        self.size = size
        self.color = color
    }
}

这就是结构体。

class Sneakers {
    static let shared = Sneakers()
    var bagProducts = [
        BagProducts(image: UIImage(named: "Image")!, category: "Men", name: "Adidas Gazelle", price: 100.00, size: 40, color: "Blue"),
        BagProducts(image: UIImage(named: "Image")!, category: "Men", name: "Adidas Gazelle", price: 100.00, size: 40, color: "Blue"),
    ]
    
    var wishProducts = [
        WishProducts(image: UIImage(named: "AdidasGalaxy")!, category: "Women", name: "Adidas Galaxy 6", price: 55.00),
        WishProducts(image: UIImage(named: "NewBalance")!, category: "Men", name: "New Balance BB550", price: 150.00),
    ]
}

最后是数组类。
你们能想到什么吗?我试了很多方法,但似乎都不起作用。

l2osamch

l2osamch1#

您可以做的一个改进是对这两种类型都使用枚举:

struct Shoe {
    var image: UIImage?
    var category: String
    var name: String
    var price: Double
    let size: Int
    let color: String
}

final class Sneakers {
    enum Product {
        case bag(shoe: Shoe)
        case wish(shoe: Shoe)
    }
    static let shared = Sneakers()
    var products: [Product] = [
        .bag(shoe: .init(image: UIImage(named: "Image"), category: "Men", name: "Adidas Gazelle", price: 100.00, size: 40, color: "Blue")),
        .wish(shoe: .init(image: UIImage(named: "Image"), category: "Men", name: "Nike Air", price: 150.00, size: 42, color: "White"))
    ]
}

现在你已经在同一个数组中得到了两种类型,如果你想得到一个只有一种类型的数组,那么你可以filter它。
我也会尽量避免使用共享示例。我可以工作得很好,但如果可能的话,我会避免使用它。在这里阅读更多:https://www.swiftbysundell.com/articles/avoiding-singletons-in-swift/

相关问题