我正在开发一个应用程序,其中用户可以访问不同的列表。我将展示一些图片以便更好地理解。
这是我的“主菜单”,用户可以在其中将自定义单元格
(“Main Wishlist”、“List1”、List2”,..)添加到UICollectionView
>.
每个cell
最终都应该“包含”自己的View
,其中包含一个TableView
。单击单元格
即可显示 View
:
这就是我让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/