Swift - 创建自定义视图的不同实例 [英] Swift - create different instances of custom Views

查看:25
本文介绍了Swift - 创建自定义视图的不同实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用户可以访问不同列表的应用.我将展示一些图片以便更好地理解.

I am working on an app in which the user has access to different lists. I will show some pictures for a better understanding.

这是我的主菜单",用户可以在其中添加自定义 cells(Main Wishlist"、List1"、List2"等)到 UICollectionView.

This is my "Main-Menue" where the user can add custom cells ("Main Wishlist", "List1", List2",..) to a UICollectionView.

每个 cell 最终都应该包含"自己的 ViewTableView.View 通过点击 cell 出现:

Each cell should in the end "contain" its own View with a TableView inside of it. The View appears by clicking on the cell:

这就是我让 View 出现的方式:

This is how I let the View appear:

    @IBAction func createListButtonTapped(_ sender: Any) {

    // "Liste erstellen" button was tapped
    self.appDidEnterBackgroundHandler()

    if let txt = listNameTextfield.text {

        self.newListTextfield.resignFirstResponder()

        // append user-entered text to the data array
        self.theData.append(txt)
        self.imageData.append(self.image!)

        self.view.addSubview(self.theCustomWishlistView)
        // constrain CustomWishlistView
        self.theCustomWishlistView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 120.0).isActive = true
        self.theCustomWishlistView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
        self.theCustomWishlistView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30.0).isActive = true
        self.theCustomWishlistView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -30.0).isActive = true
        self.theCustomWishlistView.wishlistImage.image = self.image
        self.theCustomWishlistView.wishlistLabel.text = txt
        self.theCustomWishlistView.transform = CGAffineTransform(translationX: 0, y: 1000)
        self.view.bringSubviewToFront(containerView)


        // reload the collection view
        theCollectionView.reloadData()
        theCollectionView.performBatchUpdates(nil, completion: {
            (result) in
            // scroll to make newly added row visible (if needed)
            let i = self.theCollectionView.numberOfItems(inSection: 0) - 1
            let idx = IndexPath(item: i, section: 0)
            self.theCollectionView.scrollToItem(at: idx, at: .bottom, animated: true)

            // close (hide) the "New List" view
            self.closeButtonTappedNewList(nil)
        })
    }
}

问题: 就像我之前说的,应该为每个单元格创建一个单独的 View,但目前每个 cell 都包含相同的 customWishlistView.

Problem: Like I previously said, there should be an individual View be created for each cell but at the moment every cell contains the same customWishlistView.

这就是我创建我的 customWishlistView 的方式:

This is how I create my customWishlistView:

lazy var theCustomWishlistView: CustomWishlistView = {
    let v = CustomWishlistView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.backgroundColor = .darkGray
    v.layer.cornerRadius = 30
    return v
}()

推荐答案

更改

lazy var theCustomWishlistView: CustomWishlistView = {
    let v = CustomWishlistView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.backgroundColor = .darkGray
    v.layer.cornerRadius = 30
    return v
}()

func createCustomWishlistView() -> CustomWishlistView {
    let v = CustomWishlistView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.backgroundColor = .darkGray
    v.layer.cornerRadius = 30
    return v
}

并使用它

代替 self.theCustomWishlistView

为每个单元格创建它:

let theCustomWishlistView = createCustomWishlistView()
...

这篇关于Swift - 创建自定义视图的不同实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆