gpt4 book ai didi

ios - 从 url 获取文本的 Swift 标签在滚动 collectionView 时会改变大小

转载 作者:行者123 更新时间:2023-11-29 05:32:18 24 4
gpt4 key购买 nike

设置:
CollectionView 中有多个单元格。它们都有一个 genreLabel(所有单元格的文本都不同)和一个 discountLabel

我期望发生的事情:
每个genreLabel足够宽以显示相应的文本,并且滚动后不会改变其大小。此外,discountLabel 在滚动后不应消失。

实际发生的情况:对于第一个单元格,文本按照我的预期显示,但是当我在 CollectionView 中向下滚动时,一些 genreLabel 变得比文本宽,所以文本不再完全显示。函数setupViews()用于在CollectionViewCell中创建约束,并在函数configure()中设置属性。此外还有第二个问题:当 genreLabel 变小时,discountLabel 也会消失。

我已经尝试过的:
我尝试将 setupViews() 的调用放在不同的地方。当我将其放入 CollectionViewCellinit() 时,genreLabel 非常小,它只显示文本的一个字母。当我将它放在 configure() 的末尾或 collectionView(cellForItemAt) 中的 cell.configure() 之后时,我遇到了问题现在。

CollectionViewController:

class HomeController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, HomeControllerDelegate {

lazy var eventCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 25
layout.minimumLineSpacing = 25
let cv = UICollectionView(frame: CGRect(x: 25, y: 108, width: UIScreen.main.bounds.width - 50, height: UIScreen.main.bounds.height - 108), collectionViewLayout: layout)
cv.dataSource = self
cv.delegate = self
return cv
}()

var homeEvents = [HomeEvent]()

let homeCellId = "HomeCellId"

override func viewDidLoad() {
super.viewDidLoad()

//Setting up the NavigationController...

HomeModel.shared.getEvents {
(response) in
self.homeEvents = response.homeEvents
self.eventCollectionView.reloadData()
}
eventCollectionView.register(HomeCell.self, forCellWithReuseIdentifier: homeCellId)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
imageCache.removeAllObjects()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return homeEvents.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = eventCollectionView.dequeueReusableCell(withReuseIdentifier: homeCellId, for: indexPath) as? HomeCell
else {
return UICollectionViewCell()
}
let event = homeEvents[indexPath.item]
cell.configure(with: event)
// cell.setupViews()
cell.homeControllerDelegate = self
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if view.frame.width < 415.0 {
return CGSize(width: view.frame.width - 50, height: view.frame.width - 60)
}
else if view.frame.width < 828.0 {
return CGSize(width: (view.frame.width / 2) - 50, height: (view.frame.width / 2) - 60)
}
else {
return CGSize(width: (view.frame.width / 3) - 50, height: (view.frame.width / 3) - 60)
}
}
}

CollectionViewCell:

class HomeCell: UICollectionViewCell {

var themePage = false
var banner = 0
var gift = 0
var teamEvent = 0
var discount = 1

func configure(with homeEvent: HomeEvent) {
homeEvent.image {
(image) in
self.eventImageView.image = image
}
genreLabel.text = homeEvent.genre
if homeEvent.teamEvent == "0" {
teamEventLabel.isHidden = true
}
else {
teamEvent = 1
teamEventLabel.isHidden = false
}
if homeEvent.gift == "0" {
giftLabel.isHidden = true
}
else {
gift = 1
giftLabel.isHidden = false
}
if homeEvent.discount.prefix(1) == "<" {
let normalText = "Bis zu "
let boldText = "" + homeEvent.discount.suffix(2) + "%"
let normalAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)]
let attributedString = NSMutableAttributedString(string: normalText, attributes: normalAttributes)
let boldAttributes = [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 24)]
let boldString = NSMutableAttributedString(string: boldText, attributes: boldAttributes)
attributedString.append(boldString)
variableDiscountLabel.attributedText = attributedString
fixDiscountLabel.isHidden = true
discount = 2
}
else {
fixDiscountLabel.text = homeEvent.discount.suffix(2) + "%"
variableDiscountLabel.isHidden = true
}
if homeEvent.bannerText == "" {
bannerView1.isHidden = true
bannerView2.isHidden = true
bannerView3.isHidden = true
bannerView4.isHidden = true
}
else {
if homeEvent.bannerText == "Angebot endet heute!" {
banner = 1
}
else if homeEvent.bannerText == "Überregionales Top-Angebot" {
banner = 2
}
else if homeEvent.bannerText == "Nur noch wenige Tickets" {
banner = 3
}
else if homeEvent.bannerText == "Angebot endet morgen!" {
banner = 4
}
else {
bannerView1.isHidden = true
bannerView2.isHidden = true
bannerView3.isHidden = true
bannerView4.isHidden = true
}
}
eventTitleLabel.text = homeEvent.title
eventSubTitleLabel.text = homeEvent.subtitle
if homeEvent.themePage == "1" {
themePage = true
locationImageView.isHidden = true
eventDatesLabel.isHidden = true
eventLocationLabel.isHidden = true
}
else {
eventDatesLabel.text = homeEvent.dates
eventLocationLabel.text = homeEvent.location
}
setupViews()
}

override func prepareForReuse() {
eventImageView.image = nil
genreLabel.text = nil
teamEventLabel.isHidden = true
giftLabel.isHidden = true
fixDiscountLabel.text = nil
variableDiscountLabel.text = nil
eventTitleLabel.text = nil
eventSubTitleLabel.text = nil
eventDatesLabel.text = nil
eventLocationLabel.text = nil
}

weak var homeControllerDelegate: HomeControllerDelegate?

override init(frame: CGRect) {
super.init(frame: frame)
// setupViews() //genreLabel just displaying one Letter
}

func setupViews() {
addSubview(genreLabel)
if discount == 1 {
addSubview(fixDiscountLabel)
}
else {
addSubview(variableDiscountLabel)
}

//setting Subviews for other labels, views ...

_ = genreLabel.anchor(topAnchor, left: leftAnchor, bottom: nil, right: nil, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: genreLabel.intrinsicContentSize.width, heightConstant: 30)

if discount == 1 {
_ = fixDiscountLabel.anchor(topAnchor, left: nil, bottom: nil, right: rightAnchor, topConstant: imageHeight - 32, leftConstant: 0, bottomConstant: 0, rightConstant: 8, widthConstant: 64, heightConstant: 64)

_ = eventTitleLabel.anchor(eventImageView.bottomAnchor, left: leftAnchor, bottom: nil, right: fixDiscountLabel.leftAnchor, topConstant: 6, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: (UIScreen.main.bounds.width - 60 - imageHeight) / 4 - 6)
}
else {
_ = variableDiscountLabel.anchor(topAnchor, left: nil, bottom: nil, right: rightAnchor, topConstant: imageHeight - 32, leftConstant: 0, bottomConstant: 0, rightConstant: 8, widthConstant: 64, heightConstant: 64)

_ = eventTitleLabel.anchor(eventImageView.bottomAnchor, left: leftAnchor, bottom: nil, right: variableDiscountLabel.leftAnchor, topConstant: 6, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: (UIScreen.main.bounds.width - 60 - imageHeight) / 4 - 6)
}

//setting the constraints for other labels, views ...

}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

最佳答案

尝试将标签添加到 CollectionViewCell 的 contentView 中,而不是 View 本身。

而不是:

addSubview(label)

contentView.addSubview(label)

关于ios - 从 url 获取文本的 Swift 标签在滚动 collectionView 时会改变大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57425769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com