Ios Swift-创建自定义视图的不同实例
我正在开发一个应用程序,其中用户可以访问不同的列表。为了更好地理解,我将展示一些图片 这是我的“主菜单”,用户可以在其中将自定义的Ios Swift-创建自定义视图的不同实例,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我正在开发一个应用程序,其中用户可以访问不同的列表。为了更好地理解,我将展示一些图片 这是我的“主菜单”,用户可以在其中将自定义的单元格添加到UICollectionView(“主愿望列表”、“列表1”、“列表2”…) 每个单元格最后应“包含”其自己的视图,并在其内部包含一个表格视图。单击单元格,即可显示视图: 这就是我让视图出现的方式: @IBAction func createListButtonTapped(_ sender: Any) { // "Liste ers
单元格添加到UICollectionView
(“主愿望列表”、“列表1”、“列表2”…)
每个单元格
最后应“包含”其自己的视图
,并在其内部包含一个表格视图
。单击单元格
,即可显示视图
:
这就是我让视图出现的方式:
@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)
})
}
}
问题:正如我前面所说,应该为每个单元格创建一个单独的视图
,但目前每个单元格
都包含相同的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
}()
到
并使用它
而不是self.customwishlistview
为每个单元格创建它:
let theCustomWishlistView = createCustomWishlistView()
...
您在哪里创建customwishlistview
变量?目前,我认为它在您的UIViewController
上,因此您基本上是在每个单元格中添加相同的视图。是的,在我的UIViewController
中。那么我可以做些什么来更改它呢?将customwishlistview
的创建移动到正确的位置,然后再开始重新将其添加到您的视图中。它应该足够了。您的意思是在函数内部吗?这不起作用,因为我需要在函数外部访问它aswell@Chris创建一个返回CustomWishlistView的方法。当前代码生成共享实例。看起来它可以工作,但我遇到了一个问题,因为我需要访问CustomWishlistViewode>在另一个函数中,因为我需要在它上执行cGraffeTransform
。@Chris这是另一个问题。你应该正确组织代码。视图控制器不应该做你提到的那样的事情-视图转换。也许你可以将其放入CustomWishlistView本身。哦,好的:/我刚才问了一个问题。也许你可以请看一看:)
let theCustomWishlistView = createCustomWishlistView()
...