我对Swift和程序设计非常陌生,请耐心等待:
我创建了一个NSObject来访问多个文本和按钮。
import UIKit
class WelcomeScreen: NSObject {
var messageText: String?
var imageName: String?
var buttonName: UIButton?
static func sampleScreens() -> [WelcomeScreen] {
let splitButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Split", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont(name: "IBMPlexSans", size: 25)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(red: 241/255, green: 249/255, blue: 254/255, alpha: 1.0) /* #f1f9fe */
button.layer.cornerRadius = 33
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
button.tag = 0
return button
}()
let playButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Play", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont(name: "IBMPlexSans", size: 25)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(red: 241/255, green: 249/255, blue: 254/255, alpha: 1.0) /* #f1f9fe */
button.layer.cornerRadius = 33
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
button.tag = 1
return button
}()
let splitScreen = WelcomeScreen()
splitScreen.messageText = "It's simple, start splitting your bill"
splitScreen.imageName = "SplitButton"
splitScreen.buttonName = splitButton
let otherScreen = WelcomeScreen()
otherScreen.messageText = "Choose a random party member to pay for the whole meal"
otherScreen.imageName = "PlayButton"
otherScreen.buttonName = playButton
return [splitScreen, otherScreen]
}
}
在这里,我可以访问文本和图像,但无法访问按钮。
class ButtonCell: UICollectionViewCell {
var button: WelcomeScreen? {
didSet {
if let text = button?.messageText {
descriptionText.text = text
}
if let imageName = button?.imageName{
splitButtonView.image = UIImage(named: imageName)
}
splitButton = button?.buttonName
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setUpView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let splitButtonView: UIImageView = {
let imageView = UIImageView(image: #imageLiteral(resourceName: "SplitButton"))
//enables autolayout for our imageView
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
return imageView
}()
var splitButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Split", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont(name: "IBMPlexSans", size: 25)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(red: 241/255, green: 249/255, blue: 254/255, alpha: 1.0) /* #f1f9fe */
button.layer.cornerRadius = 33
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
return button
}()
let playButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Play", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont(name: "IBMPlexSans", size: 25)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(red: 241/255, green: 249/255, blue: 254/255, alpha: 1.0) /* #f1f9fe */
button.layer.cornerRadius = 33
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
return button
}()
private let descriptionText: UILabel = {
let textLabel = UILabel()
textLabel.text = "It's simple, start splitting your app"
textLabel.font = UIFont(name: "IBMPlexSans-Light", size: 20)
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.textAlignment = .center
textLabel.backgroundColor = .clear
textLabel.textColor = UIColor.gray
textLabel.lineBreakMode = .byWordWrapping
textLabel.numberOfLines = 2
return textLabel
}()
func setUpView(){
backgroundColor = UIColor.clear
addSubview(descriptionText)
addSubview(splitButtonView)
addSubview(splitButton)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-30-[v0]-30-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": descriptionText]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-30-[v0]-30-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": splitButton]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-50-[v1(==70)][v0(==70)]-40-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": splitButton, "v1": descriptionText]))
}
}
我的程序有多个单元格,不同的文本和图像出现在不同的单元格上。我试图弄清楚如何在不同的单元格上显示我的按钮。以下是屏幕截图:
我希望我说清楚了。任何帮助将非常感激。谢谢!
P.S。这些代码大部分来自YouTube上的各种快速教程。
答案 0 :(得分:0)
我认为创建自定义集合视图单元比创建自定义对象会更好,更容易。在为Swift开发ios的所有时间里,我只需要在处理目标c时从NSObject实例化。