[英]Swift UICollectionViewCell didSelectItemAt not printing label name on cell
[英]Swift - pick image and label for UICollectionViewCell
現在我有一個collectionView,我可以通過點擊“addCell”在其中添加單元格。
我的目標是,如果用戶點擊“addCell”,應該會出現一個視圖,用戶可以在其中輸入單元格的標題和 select 的圖像,如下所示:
關於我如何實現這一點的任何想法?
class ContentCell: UICollectionViewCell {
let testImage: UIImageView = {
let v = UIImageView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .cyan
return v
}()
let testLabel: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.text = "Test Label"
v.font = UIFont(name: "Avenir Next-Bold", size: 18)
v.textColor = .darkGray
v.textAlignment = .center
return v
}()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() -> Void {
contentView.addSubview(testLabel)
contentView.addSubview(testImage)
NSLayoutConstraint.activate([
testImage.topAnchor.constraint(equalTo: contentView.topAnchor),
testImage.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
testImage.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
testImage.heightAnchor.constraint(equalToConstant:150),
testLabel.topAnchor.constraint(equalTo: testImage.bottomAnchor,constant: 1),
testLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
testLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
testLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
])
}
}
so in didSelectItemAtIndexPath
you present the view controller to ask for the image image and title, and then in that view controller you declare a delegate or completion handler that when the user presses "done" or "save" in the view controller where they selected the圖像和標題,然后調用委托或完成處理程序,然后用單元格告訴原始視圖 controller 添加您剛剛創建的項目並重新加載 collectionView。 這是非常標准的東西,並且您需要學習如何使用相同的模式,因為大多數應用程序每個應用程序使用相同的模式 100 次。 我可以從 Objective-C 向您展示,但不會浪費我的時間來展示這是 Swift。 lmk 如果您需要更多幫助。 祝你好運
哦,是的,為了將來參考,最好堅持使用委托,因為當您遇到嵌套完成的情況時使用完成處理程序會變得非常棘手,並且仍然需要維護對 controller 的弱化然后強化的引用。 在弱化和強化“自我”兩級之后,系統將開始隨機釋放您的控制器。 堅持委托。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.