ios - Swift - 创建自定义 View 的不同实例

标签 ios swift uitableview uicollectionview

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

这是我的“主菜单”,用户可以在其中将自定义单元格(“Main Wishlist”、“List1”、List2”,..)添加到UICollectionView >.

enter image description here

每个cell最终都应该“包含”自己的View,其中包含一个TableView。单击单元格即可显示 View :

enter image description here

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

    @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

这就是我创建 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()
...

关于ios - Swift - 创建自定义 View 的不同实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59161628/

相关文章:

ios - 使用 Alamofire 将嵌套数组从 JSON 数据显示到 TableView

ios - NSMutableSet 与自定义 isEqual : and hash callbacks

ios - 重复向 `UITableViewCell` 添加 subview

ios - 锁定时 AVAudioPlayer 在 iPhone 5 中不播放音频

ios - 如何截断 UITableView Cell TextLabel 中的文本,使其不隐藏 DetailTextLabel?

ios - 自定义 UITableViewCell : programmatically trigger selection segue

ios - 导航中实例的不确定性 : UITabBarController

arrays - 如何使用 iOS Swift 将一组图像存储在 Firebase Storage 中的 1 个路径下

swift - 如何从 firestore 文档中检索 key (使用 swift 4/ios)

macos - Swift 中的 CommonHMAC