Ios Swift-在一个视图中加载不同的视图
我正在开发一个Ios Swift-在一个视图中加载不同的视图,ios,swift,class,uiview,Ios,Swift,Class,Uiview,我正在开发一个愿望列表应用程序,我遇到了一个需要解释的问题: 了解更多信息的图片: let wishlistView: UIView = { let v = UIView() v.translatesAutoresizingMaskIntoConstraints = false v.backgroundColor = .darkGray v.layer.cornerRadius = 30 return v }() lazy var theTableVie
愿望列表应用程序
,我遇到了一个需要解释的问题:
了解更多信息的图片:
let wishlistView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .darkGray
v.layer.cornerRadius = 30
return v
}()
lazy var theTableView: WhishlistTableViewController = {
let v = WhishlistTableViewController()
v.view.layer.masksToBounds = true
v.view.layer.borderColor = UIColor.white.cgColor
v.view.backgroundColor = .clear
v.view.layer.borderWidth = 7.0
v.view.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let dismissWishlistViewButton: UIButton = {
let v = UIButton()
v.setImage(UIImage(named: "dropdown"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
v.addTarget(self, action: #selector(hideView), for: .touchUpInside)
return v
}()
let menueButton: UIButton = {
let v = UIButton()
v.setImage(UIImage(named: "menueButton"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
v.addTarget(self, action: #selector(menueButtonTapped), for: .touchUpInside)
return v
}()
let wishlistLabel: UILabel = {
let v = UILabel()
v.text = "Main Wishlist"
v.font = UIFont(name: "AvenirNext-Bold", size: 30)
v.textColor = .white
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let wishCounterLabel: UILabel = {
let v = UILabel()
v.text = "5 unerfüllte Wünsche"
v.font = UIFont(name: "AvenirNext", size: 12)
v.textColor = .white
v.font = v.font.withSize(12)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let wishlistImage: UIImageView = {
let v = UIImageView()
v.image = UIImage(named: "iconRoundedImage")
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainWishlistCell", for: indexPath) as! MainWishlistCell
cell.wishlistTapCallback = {
// let wishlistView appear
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.wishlistView.transform = CGAffineTransform(translationX: 0, y: 0)
})
// let welcomeText disappear
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
self.welcomeTextLabel.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
return cell
}
else if indexPath.item <= theData.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell
cell.testLabel.text = theData[indexPath.item - 1]
cell.testImage.image = self.image
cell.testImage.image = imageData[indexPath.item - 1]
return cell
}
// past the end of the data count, so return an "Add Item" cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddItemCell", for: indexPath) as! AddItemCell
// set the closure
cell.tapCallback = {
self.listNameTextfield.becomeFirstResponder()
// let newListView appear
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.blurrImage.alpha = 0.96
self.blurrImage.transform = CGAffineTransform(translationX: 0, y: 0)
self.newListView.transform = CGAffineTransform(translationX: 0, y: 0)
self.view.layoutIfNeeded()
})
}
return cell
}
我的主页(第一张图片)包含一个CollectionView
,用户可以在其中添加项目(“不同的愿望列表”),然后看起来像第二张图片:
此时,用户可以单击“主愿望列表”,该列表如下所示:
用户可以“添加愿望”(点击底部的+按钮),然后将其添加到“主愿望列表”TableView
代码:
let wishlistView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .darkGray
v.layer.cornerRadius = 30
return v
}()
lazy var theTableView: WhishlistTableViewController = {
let v = WhishlistTableViewController()
v.view.layer.masksToBounds = true
v.view.layer.borderColor = UIColor.white.cgColor
v.view.backgroundColor = .clear
v.view.layer.borderWidth = 7.0
v.view.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let dismissWishlistViewButton: UIButton = {
let v = UIButton()
v.setImage(UIImage(named: "dropdown"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
v.addTarget(self, action: #selector(hideView), for: .touchUpInside)
return v
}()
let menueButton: UIButton = {
let v = UIButton()
v.setImage(UIImage(named: "menueButton"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
v.addTarget(self, action: #selector(menueButtonTapped), for: .touchUpInside)
return v
}()
let wishlistLabel: UILabel = {
let v = UILabel()
v.text = "Main Wishlist"
v.font = UIFont(name: "AvenirNext-Bold", size: 30)
v.textColor = .white
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let wishCounterLabel: UILabel = {
let v = UILabel()
v.text = "5 unerfüllte Wünsche"
v.font = UIFont(name: "AvenirNext", size: 12)
v.textColor = .white
v.font = v.font.withSize(12)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let wishlistImage: UIImageView = {
let v = UIImageView()
v.image = UIImage(named: "iconRoundedImage")
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainWishlistCell", for: indexPath) as! MainWishlistCell
cell.wishlistTapCallback = {
// let wishlistView appear
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.wishlistView.transform = CGAffineTransform(translationX: 0, y: 0)
})
// let welcomeText disappear
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
self.welcomeTextLabel.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
return cell
}
else if indexPath.item <= theData.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell
cell.testLabel.text = theData[indexPath.item - 1]
cell.testImage.image = self.image
cell.testImage.image = imageData[indexPath.item - 1]
return cell
}
// past the end of the data count, so return an "Add Item" cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddItemCell", for: indexPath) as! AddItemCell
// set the closure
cell.tapCallback = {
self.listNameTextfield.becomeFirstResponder()
// let newListView appear
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.blurrImage.alpha = 0.96
self.blurrImage.transform = CGAffineTransform(translationX: 0, y: 0)
self.newListView.transform = CGAffineTransform(translationX: 0, y: 0)
self.view.layoutIfNeeded()
})
}
return cell
}
我的“主愿望列表”-单元格
在它自己的自定义类中,用户可以添加的所有其他“愿望列表”(“TestList”、“TestList2”)都是一个类
点击“主愿望列表”单元格后出现的TableView
并不是另一个ViewController
。它只是一个视图
,我让它随View.transoform
一起出现/消失
这是我的“愿望视图”(第三张图片):
let wishlistView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .darkGray
v.layer.cornerRadius = 30
return v
}()
lazy var theTableView: WhishlistTableViewController = {
let v = WhishlistTableViewController()
v.view.layer.masksToBounds = true
v.view.layer.borderColor = UIColor.white.cgColor
v.view.backgroundColor = .clear
v.view.layer.borderWidth = 7.0
v.view.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let dismissWishlistViewButton: UIButton = {
let v = UIButton()
v.setImage(UIImage(named: "dropdown"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
v.addTarget(self, action: #selector(hideView), for: .touchUpInside)
return v
}()
let menueButton: UIButton = {
let v = UIButton()
v.setImage(UIImage(named: "menueButton"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
v.addTarget(self, action: #selector(menueButtonTapped), for: .touchUpInside)
return v
}()
let wishlistLabel: UILabel = {
let v = UILabel()
v.text = "Main Wishlist"
v.font = UIFont(name: "AvenirNext-Bold", size: 30)
v.textColor = .white
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let wishCounterLabel: UILabel = {
let v = UILabel()
v.text = "5 unerfüllte Wünsche"
v.font = UIFont(name: "AvenirNext", size: 12)
v.textColor = .white
v.font = v.font.withSize(12)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let wishlistImage: UIImageView = {
let v = UIImageView()
v.image = UIImage(named: "iconRoundedImage")
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainWishlistCell", for: indexPath) as! MainWishlistCell
cell.wishlistTapCallback = {
// let wishlistView appear
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.wishlistView.transform = CGAffineTransform(translationX: 0, y: 0)
})
// let welcomeText disappear
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
self.welcomeTextLabel.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
return cell
}
else if indexPath.item <= theData.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell
cell.testLabel.text = theData[indexPath.item - 1]
cell.testImage.image = self.image
cell.testImage.image = imageData[indexPath.item - 1]
return cell
}
// past the end of the data count, so return an "Add Item" cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddItemCell", for: indexPath) as! AddItemCell
// set the closure
cell.tapCallback = {
self.listNameTextfield.becomeFirstResponder()
// let newListView appear
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.blurrImage.alpha = 0.96
self.blurrImage.transform = CGAffineTransform(translationX: 0, y: 0)
self.newListView.transform = CGAffineTransform(translationX: 0, y: 0)
self.view.layoutIfNeeded()
})
}
return cell
}
并且它在用户点击“主愿望列表”-单元格后出现,如下所示:
let wishlistView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .darkGray
v.layer.cornerRadius = 30
return v
}()
lazy var theTableView: WhishlistTableViewController = {
let v = WhishlistTableViewController()
v.view.layer.masksToBounds = true
v.view.layer.borderColor = UIColor.white.cgColor
v.view.backgroundColor = .clear
v.view.layer.borderWidth = 7.0
v.view.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let dismissWishlistViewButton: UIButton = {
let v = UIButton()
v.setImage(UIImage(named: "dropdown"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
v.addTarget(self, action: #selector(hideView), for: .touchUpInside)
return v
}()
let menueButton: UIButton = {
let v = UIButton()
v.setImage(UIImage(named: "menueButton"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
v.addTarget(self, action: #selector(menueButtonTapped), for: .touchUpInside)
return v
}()
let wishlistLabel: UILabel = {
let v = UILabel()
v.text = "Main Wishlist"
v.font = UIFont(name: "AvenirNext-Bold", size: 30)
v.textColor = .white
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let wishCounterLabel: UILabel = {
let v = UILabel()
v.text = "5 unerfüllte Wünsche"
v.font = UIFont(name: "AvenirNext", size: 12)
v.textColor = .white
v.font = v.font.withSize(12)
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
let wishlistImage: UIImageView = {
let v = UIImageView()
v.image = UIImage(named: "iconRoundedImage")
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainWishlistCell", for: indexPath) as! MainWishlistCell
cell.wishlistTapCallback = {
// let wishlistView appear
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.wishlistView.transform = CGAffineTransform(translationX: 0, y: 0)
})
// let welcomeText disappear
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
self.welcomeTextLabel.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
return cell
}
else if indexPath.item <= theData.count {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell
cell.testLabel.text = theData[indexPath.item - 1]
cell.testImage.image = self.image
cell.testImage.image = imageData[indexPath.item - 1]
return cell
}
// past the end of the data count, so return an "Add Item" cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddItemCell", for: indexPath) as! AddItemCell
// set the closure
cell.tapCallback = {
self.listNameTextfield.becomeFirstResponder()
// let newListView appear
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.blurrImage.alpha = 0.96
self.blurrImage.transform = CGAffineTransform(translationX: 0, y: 0)
self.newListView.transform = CGAffineTransform(translationX: 0, y: 0)
self.view.layoutIfNeeded()
})
}
return cell
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
如果indexath.item==0{
让cell=collectionView.dequeueReuseAbleCell(带有ReuseIdentifier:“MainWishlistCell”,for:indexPath)作为!MainWishlistCell
cell.wishlistTapCallback={
//让wishlistView出现
UIView.animate(持续时间:0.3,延迟:0,选项:.curveEaseIn,动画:{
self.wishlistView.transform=CGAffineTransform(translationX:0,y:0)
})
//让welcomeText消失
UIView.animate(持续时间:0.2,延迟:0,选项:.curveEaseOut,动画:{
self.welcomeTextLabel.transform=CGAffineTransform(translationX:0,y:0)
})
}
返回单元
}
否则,如果indexPath.item只需在那里实现另一个UItableViewController,可能使用容器视图。它将包含的视图将是您的测试列表(UItableViewController)是的,您可以在collectionview单元格中拥有tableView和其他视图-至少我是这样解释您的问题的。请参见此处的示例:不,这不是我的问题。我的问题是:我有一个工作的tableView类,但它是为“主愿望列表”专门实现的(标签、图像等)“。但是我想用我的collectionView
中的所有其他单元格动态使用这个tableView
。这有意义吗?”?