【发布时间】:2018-06-12 16:59:27
【问题描述】:
因此,我正在尝试寻找新的方法来保持我的项目整洁,并让我自己更容易管理。
我希望这样做的方式是将所有关系都保存在它自己的 swift 文件中,然后在需要时调用它。
我会尽力安排我的问题,但如果屏幕截图有帮助,或者如果您需要任何其他信息,我会尽力提供。
需要注意的一点:应用程序首次启动时会加载集合视图,这是应该的。并且 MenuActionLabel.text 出口,设置为:'Hi'。
但是当我尝试调用 ShowMe() 时,CollectionView 没有重新加载,而是崩溃了,因为 Collectioner 返回了 nil optional。
import UIKit
class UserMenu: UIView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
struct Variables {
static var myName = "Hi"
}
@IBOutlet weak var Collectioner: UICollectionView!
当我调用以下“ShowMe()”函数时,我得到了“coll: nil”的打印结果。
我从 ViewController.swift 调用函数,通过 'UserMenu().ShowMe()'
func ShowMe() {
Variables.myName = "Justin"
print("coll: \(self.Collectioner)")
//Collectioner.reloadData()
//^---- causes error - see didSelectItem for my problem...
//returns nil optional...? Why?
}
//testing to find errors
func numberOfSections(in collectionView: UICollectionView) -> Int {
if collectionView == Collectioner {
return 1
} else {
return 0
}
}
//testing to find errors
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == Collectioner {
return 1
} else {
return 0
}
}
//testing to find errors
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == Collectioner {
let Cell:UserMenuCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! UserMenuCell
Cell.MenuActionLabel.text = "\(Variables.myName)"
return Cell
} else {
let Cell:UserMenuCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! UserMenuCell
return Cell
}
}
//testing to find errors
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
cell.alpha = 0
UIView.animate(withDuration: 0.8) {
cell.alpha = 1
}
}
//testing to find errors
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView == Collectioner {
return CGSize(width: Collectioner.frame.width, height: 50)
} else {
return CGSize(width: Collectioner.frame.width, height: 50)
}
}
//testing to find errors
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
if collectionView == Collectioner {
return 0
} else {
return 0
}
}
我的困惑出现在这里:
/*
When I tap on the cell:
1. The collection view reloads, and the updated data is shown.
2. Yet... if I call reloadData from elsewhere,
the Collectioner outlet returns as an optional nil?
*/
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == Collectioner {
print(collectionView)
print(" ")
print(Collectioner)
print(" ")
collectionView.reloadData()
}
}
}
didSelectItemAt 的打印输出是:
<UICollectionView: 0x7fd13103a000; frame = (0 0; 314 716); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x60c000242a00>; layer = <CALayer: 0x60c00022d140>; contentOffset: {0, 0}; contentSize: {314, 50}; adjustedContentInset: {0, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7fd12ef1b450>
some(<UICollectionView: 0x7fd13103a000; frame = (0 0; 314 716); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x60c000242a00>; layer = <CALayer: 0x60c00022d140>; contentOffset: {0, 0}; contentSize: {314, 50}; adjustedContentInset: {0, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7fd12ef1b450>)
并且集合视图闪烁,表示它已重新加载。
那么,当我尝试从其他任何地方重新加载集合视图时,为什么它返回 nil?
请帮忙。这让我疯狂。我已经尝试了所有我能想到的方法,并且为寻找答案、解决方案做了很多工作,并且乐于接受建议和学习。
我不确定是否需要在启动时以某种方式“确认”IBOutlet,即使一切似乎都通过 StoryBoard 正确连接。
编辑 - ViewController.swift:
import UIKit
class ViewController: UIViewController {
let userMenu = UserMenu()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBOutlet weak var button: UIButton!
@IBAction func button(_ sender: Any) {
print("Tapped Button")
UserMenu().ShowMe()
}
}
干杯,
贾斯汀。
【问题讨论】:
标签: ios swift xcode uiview uicollectionview