编辑:我正在尝试重新创建Instagram / twitter个人资料页面。因此,如果我尝试这样做的当前方式不起作用,请随时告诉我如何以完全不同的方式重新创建这些配置文件页面。
我已经设置了一个带标题的UICollectionView。在这个标题内,有一个menuBar有两个不同的选择选项。如果用户选择第一个索引,则menuBar返回0.如果用户选择第二个索引,则menuBar返回1.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CoverCell
return cell
}
下面是菜单栏及其下方的collectionview单元格的图片:
我也有这个代码来设置单元格:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let menuBarIndex = profileMenuBarSelectionIndex.menuBarIndexSelection
var cell: UICollectionViewCell
switch menuBarIndex {
case 0:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: test1CellId, for: indexPath) as! test1Cell
case 1:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: test2CellId, for: indexPath) as! test2Cell
default:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: test3CellId, for: indexPath) as! test3Cell
}
return cell
}
编辑2:我现在添加了此代码,其中myTestAction
是testAction
在不同文件中的委托函数。
var index: Int = 0
func testAction() {
index = index == 1 ? 0 : 1
collectionView.reloadData()
}
func myTestAction() {
delegate?.testAction()
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch indexPath.row{
case 0:
menuBarIndexSelection = 0
menuBarIndexChangeFlag = 1
case 1:
menuBarIndexSelection = 1
menuBarIndexChangeFlag = 2
case 2:
menuBarIndexSelection = 2
menuBarIndexChangeFlag = 3
default:
menuBarIndexSelection = 0
}
myTestAction()
}
我希望能够在标题下方显示不同的单元格,具体取决于menuBar返回的值。我该怎么做? 要么 如何重新创建Instagram个人资料页面?
答案 0 :(得分:0)
检查cellForRow和suppviewsViewsForHeader以获取menuBar值。当menuBar的值改变时,只需重新加载表。在重新加载时,数据将根据您在dataSources中的检查显示。
// MENU BAR BUTTON CLICK
self.collectionView.reloadData()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
if menuBar == 0 {
// show this image
}else{
//show that image
}
return cell
}
答案 1 :(得分:0)
试试这个,
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//Take a variable menuBar. when the user tap change it's value to 0 or 1 and reload collection view when user tap on menu button.
if menuBar == 0 {
return 0
}else{
return 1
}
答案 2 :(得分:0)
您获得编译器错误的原因是您创建了4个不同的变量,称为" cell":
var cell: UICollectionViewCell
switch menuBarIndex {
case 0:
var cell = ...
case 1:
var cell = ...
default:
var cell = ...
}
return cell
您在函数末尾返回的单元格实例从未分配过值。 在每种情况下分配第一个单元格变量而不是创建新的单元格应该修复编译器错误:
var cell: UICollectionViewCell
switch menuBarIndex {
case 0:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: test1CellId, for: indexPath) as! test1Cell
case 1:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: test2CellId, for: indexPath) as! test2Cell
default:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: test3CellId, for: indexPath) as! test3Cell
}
return cell
------------------------- EDIT ---------------------
上面的答案修正了编译器错误,但没有解决实际问题。 我创建了一个小样本项目,可以完成所需的工作并且工作正常。也许这有助于找出问题所在。我刚刚在Xcode中创建了一个视图应用程序,这是项目中单个视图控制器和单元格的代码:
class ViewController: UIViewController, UICollectionViewDataSource {
var index: Int = 0
var collectionView: UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
let btn = UIButton(frame: CGRect(x: 10, y: 10, width: 200, height: 50))
btn.backgroundColor = .green
btn.setTitle("Change cell color", for: .normal)
btn.setTitleColor(.black, for: .normal)
btn.addTarget(self, action: #selector(testAction), for: .touchUpInside)
view.addSubview(btn)
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 10
layout.minimumLineSpacing = 10
layout.scrollDirection = .vertical
collectionView = UICollectionView(frame: CGRect(x: 0, y: 60, width: view.frame.width, height: view.frame.height - 60), collectionViewLayout: layout)
collectionView?.backgroundColor = .white
collectionView?.dataSource = self
collectionView?.register(RedCell.self, forCellWithReuseIdentifier: "red")
collectionView?.register(BlueCell.self, forCellWithReuseIdentifier: "blue")
view.addSubview(collectionView!)
}
func testAction() {
index = index == 1 ? 0 : 1
collectionView?.reloadData()
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell: UICollectionViewCell
switch index {
case 0:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "red", for: indexPath)
case 1:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "blue", for: indexPath)
default:
cell = UICollectionViewCell()
}
return cell
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
return CGSize(width: 50, height: 50)
}
}
class RedCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .red
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class BlueCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .blue
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}