问题
I am working on an app in which the user has access to different lists. I will show some pictures for a better understanding.
This is my "Main-Menue" where the user can add custom cells
("Main Wishlist", "List1", List2",..) to a UICollectionView
.
Each cell
should in the end "contain" its own View
with a TableView
inside of it. The View
appears by clicking on the cell
:
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)
})
}
}
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
.
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
}()
回答1:
Change
lazy var theCustomWishlistView: CustomWishlistView = {
let v = CustomWishlistView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .darkGray
v.layer.cornerRadius = 30
return v
}()
to
func createCustomWishlistView() -> CustomWishlistView {
let v = CustomWishlistView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .darkGray
v.layer.cornerRadius = 30
return v
}
and use it
instead of self.theCustomWishlistView
create it for every cell:
let theCustomWishlistView = createCustomWishlistView()
...
来源:https://stackoverflow.com/questions/59161628/swift-create-different-instances-of-custom-views