Commit c2dff21e authored by Alex朱枝文's avatar Alex朱枝文

我的资源优化

parent 55ceadf2
...@@ -279,11 +279,6 @@ private extension YHResourceViewController { ...@@ -279,11 +279,6 @@ private extension YHResourceViewController {
} }
if !result.isEmpty { if !result.isEmpty {
// 计算每个模型的高度参数
for item in result {
item.calHeightParam()
}
self.viewModel.arrResourceData = result self.viewModel.arrResourceData = result
self.updateUI() self.updateUI()
} }
...@@ -469,7 +464,11 @@ private extension YHResourceViewController { ...@@ -469,7 +464,11 @@ private extension YHResourceViewController {
return return
} }
YHGrayCommonAlertView.show("需联系银河管家进行发布信息", "联系银河管家发布企业服务与企业需求,快来与我们联系吧~", "返回", "前往联系", fullGuestureEnable: false) {
// //
} callBack: {
//
}
} }
func selectFilterType(_ type: YHResourceFilterType) { func selectFilterType(_ type: YHResourceFilterType) {
......
...@@ -14,6 +14,8 @@ class YHResourceListModel: SmartCodable { ...@@ -14,6 +14,8 @@ class YHResourceListModel: SmartCodable {
required init() {} required init() {}
var needNewLine: Bool?
// MARK: - 基本信息 // MARK: - 基本信息
var id: String = "" var id: String = ""
var title: String = "" var title: String = ""
...@@ -162,54 +164,20 @@ class YHResourceListModel: SmartCodable { ...@@ -162,54 +164,20 @@ class YHResourceListModel: SmartCodable {
} }
// MARK: - 计算Cell高度方法 // MARK: - 计算Cell高度方法
func calHeightParam() { func didFinishMapping() {
let screenWidth = KScreenWidth if needNewLine == nil {
let cellWidth = (screenWidth - 47) / 2 needNewLine = calculateNeedNewLine()
}
var totalHeight: CGFloat = 60 + 20 + 44 // 基础高度
}
// 标题高度
if !title.isEmpty { func calculateNeedNewLine() -> Bool {
let titleFont = UIFont.PFSC_M(ofSize: 16) ?? UIFont.systemFont(ofSize: 16) let company = company_name.count > 0 ? company_name : "-"
let maxSize = CGSize(width: cellWidth - 24, height: CGFloat.greatestFiniteMagnitude) let categoryName = category_name.count > 0 ? category_name : "-"
let titleRect = title.boundingRect(
with: maxSize, let companyWidth = YHResourceTableViewCell.getLabelWidth(company)
options: [.usesLineFragmentOrigin, .usesFontLeading], let categoryNameWidth = YHResourceTableViewCell.getLabelWidth(categoryName)
attributes: [.font: titleFont], return companyWidth + categoryNameWidth >= KScreenWidth - YHResourceTableViewCell.logoWidth - YHResourceTableViewCell.logoToRight - YHResourceTableViewCell.arrowWidth - 2 * YHResourceTableViewCell.marginX - YHResourceTableViewCell.widthVLine - YHResourceTableViewCell.marginBetweenVLine * 2
context: nil
)
totalHeight += ceil(titleRect.height) + 8
}
// 内容高度
if !content.isEmpty {
let contentFont = UIFont.PFSC_R(ofSize: 13) ?? UIFont.systemFont(ofSize: 13)
let maxSize = CGSize(width: cellWidth - 24, height: CGFloat.greatestFiniteMagnitude)
let textRect = content.boundingRect(
with: maxSize,
options: [.usesLineFragmentOrigin, .usesFontLeading],
attributes: [.font: contentFont],
context: nil
)
totalHeight += min(ceil(textRect.height), 40) + 8 // 限制最大高度为2行
}
// 图片高度
if !images.isEmpty {
let imageHeight: CGFloat = 100
totalHeight += imageHeight + 8
}
// 标签高度
if !tags.isEmpty {
totalHeight += 20 + 8
}
// 底部信息高度
totalHeight += 30
self.cell_width = cellWidth
self.cell_height = max(totalHeight, 160) // 最小高度160
} }
// MARK: - 便利方法 // MARK: - 便利方法
......
...@@ -267,10 +267,10 @@ extension YHResourceCategoryView: UICollectionViewDataSource, UICollectionViewDe ...@@ -267,10 +267,10 @@ extension YHResourceCategoryView: UICollectionViewDataSource, UICollectionViewDe
selectedCategories.append(category) selectedCategories.append(category)
} }
// 如果没有选中任何分类,自动选中"全部行业" // // 如果没有选中任何分类,自动选中"全部行业"
if selectedCategories.isEmpty { // if selectedCategories.isEmpty {
selectedCategories.append(categories.first!) // selectedCategories.append(categories.first!)
} // }
} }
collectionView.reloadData() collectionView.reloadData()
......
...@@ -12,13 +12,13 @@ import Kingfisher ...@@ -12,13 +12,13 @@ import Kingfisher
class YHResourceTableViewCell: UITableViewCell { class YHResourceTableViewCell: UITableViewCell {
static let labelFont = UIFont.PFSC_R(ofSize: 14) static let labelFont = UIFont.PFSC_R(ofSize: 14)
static let labelHeight: CGFloat = 20.0 static let labelHeight: CGFloat = 20.0
private let marginX: CGFloat = 20.0 static let marginX: CGFloat = 20.0
private let logoWidth: CGFloat = 60.0 static let logoWidth: CGFloat = 60.0
private let logoToRight: CGFloat = 10.0 static let logoToRight: CGFloat = 10.0
private let marginBetweenVLine: CGFloat = 6.0 static let marginBetweenVLine: CGFloat = 6.0
private let widthVLine: CGFloat = 1 static let widthVLine: CGFloat = 1
private let arrowWidth: CGFloat = 24 static let arrowWidth: CGFloat = 24
private let arrowToLeft: CGFloat = 24 static let arrowToLeft: CGFloat = 24
static let cellReuseIdentifier = "YHResourceTableViewCell" static let cellReuseIdentifier = "YHResourceTableViewCell"
...@@ -106,9 +106,9 @@ class YHResourceTableViewCell: UITableViewCell { ...@@ -106,9 +106,9 @@ class YHResourceTableViewCell: UITableViewCell {
} }
// MARK: - 私有方法 // MARK: - 私有方法
private extension YHResourceTableViewCell { extension YHResourceTableViewCell {
func setupUI() { private func setupUI() {
backgroundColor = .white backgroundColor = .white
selectionStyle = .none selectionStyle = .none
...@@ -124,17 +124,17 @@ private extension YHResourceTableViewCell { ...@@ -124,17 +124,17 @@ private extension YHResourceTableViewCell {
setupConstraints() setupConstraints()
} }
func setupConstraints() { private func setupConstraints() {
// LOGO约束 // LOGO约束
logoImageView.snp.makeConstraints { make in logoImageView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(marginX) make.left.equalToSuperview().offset(YHResourceTableViewCell.marginX)
make.top.equalToSuperview().offset(24) make.top.equalToSuperview().offset(24)
make.width.height.equalTo(logoWidth) make.width.height.equalTo(YHResourceTableViewCell.logoWidth)
} }
// 右上角标签约束 // 右上角标签约束
typeTagIcon.snp.makeConstraints { make in typeTagIcon.snp.makeConstraints { make in
make.left.equalTo(logoImageView.snp.right).offset(logoToRight) make.left.equalTo(logoImageView.snp.right).offset(YHResourceTableViewCell.logoToRight)
make.top.equalTo(logoImageView) make.top.equalTo(logoImageView)
make.height.equalTo(16) make.height.equalTo(16)
make.width.equalTo(52) make.width.equalTo(52)
...@@ -142,14 +142,14 @@ private extension YHResourceTableViewCell { ...@@ -142,14 +142,14 @@ private extension YHResourceTableViewCell {
// 主标题约束 // 主标题约束
titleLabel.snp.makeConstraints { make in titleLabel.snp.makeConstraints { make in
make.left.equalTo(logoImageView.snp.right).offset(logoToRight) make.left.equalTo(logoImageView.snp.right).offset(YHResourceTableViewCell.logoToRight)
make.right.equalTo(rightArrow.snp.left) make.right.equalTo(rightArrow.snp.left)
make.top.equalTo(typeTagIcon.snp.bottom).offset(4) make.top.equalTo(typeTagIcon.snp.bottom).offset(4)
} }
rightArrow.snp.makeConstraints { make in rightArrow.snp.makeConstraints { make in
make.right.equalToSuperview().offset(-marginX) make.right.equalToSuperview().offset(-YHResourceTableViewCell.marginX)
make.width.height.equalTo(arrowWidth) make.width.height.equalTo(YHResourceTableViewCell.arrowWidth)
make.top.equalTo(titleLabel) make.top.equalTo(titleLabel)
} }
...@@ -160,7 +160,7 @@ private extension YHResourceTableViewCell { ...@@ -160,7 +160,7 @@ private extension YHResourceTableViewCell {
} }
vSeparatorLine.snp.makeConstraints { make in vSeparatorLine.snp.makeConstraints { make in
make.left.equalTo(companyLabel.snp.right).offset(marginBetweenVLine) make.left.equalTo(companyLabel.snp.right).offset(YHResourceTableViewCell.marginBetweenVLine)
make.centerY.equalTo(companyLabel) make.centerY.equalTo(companyLabel)
make.width.equalTo(1) make.width.equalTo(1)
make.height.equalTo(8) make.height.equalTo(8)
...@@ -168,7 +168,7 @@ private extension YHResourceTableViewCell { ...@@ -168,7 +168,7 @@ private extension YHResourceTableViewCell {
// 行业标签约束 // 行业标签约束
industryLabel.snp.makeConstraints { make in industryLabel.snp.makeConstraints { make in
make.left.equalTo(vSeparatorLine.snp.right).offset(marginBetweenVLine) make.left.equalTo(vSeparatorLine.snp.right).offset(YHResourceTableViewCell.marginBetweenVLine)
make.right.lessThanOrEqualTo(rightArrow.snp.left) make.right.lessThanOrEqualTo(rightArrow.snp.left)
make.top.equalTo(companyLabel) make.top.equalTo(companyLabel)
make.bottom.equalToSuperview().offset(-32) make.bottom.equalToSuperview().offset(-32)
...@@ -182,13 +182,13 @@ private extension YHResourceTableViewCell { ...@@ -182,13 +182,13 @@ private extension YHResourceTableViewCell {
} }
} }
private func getLabelWidth(_ text: String) -> CGFloat { static func getLabelWidth(_ text: String) -> CGFloat {
let attrString = NSAttributedString(string: text, attributes: [.font: YHResourceTableViewCell.labelFont]) let attrString = NSAttributedString(string: text, attributes: [.font: YHResourceTableViewCell.labelFont])
let width = attrString.yh_width(containerHeight: YHResourceTableViewCell.labelHeight) let width = attrString.yh_width(containerHeight: YHResourceTableViewCell.labelHeight)
return width return width
} }
func updateUI() { private func updateUI() {
guard let model = resourceModel else { return } guard let model = resourceModel else { return }
// 设置基本信息 // 设置基本信息
...@@ -198,16 +198,22 @@ private extension YHResourceTableViewCell { ...@@ -198,16 +198,22 @@ private extension YHResourceTableViewCell {
titleLabel.text = title titleLabel.text = title
companyLabel.text = company companyLabel.text = company
industryLabel.text = categoryName industryLabel.text = categoryName
let companyWidth = getLabelWidth(company) var needNext = false
let categoryNameWidth = getLabelWidth(categoryName) if let needNewLine = model.needNewLine {
if companyWidth + categoryNameWidth < KScreenWidth - logoWidth - logoToRight - arrowWidth - 2 * marginX - widthVLine - marginBetweenVLine * 2 { needNext = needNewLine
} else {
let needNewLine = model.calculateNeedNewLine()
model.needNewLine = needNewLine
needNext = needNewLine
}
if !needNext {
companyLabel.snp.remakeConstraints { make in companyLabel.snp.remakeConstraints { make in
make.left.equalTo(titleLabel) make.left.equalTo(titleLabel)
make.top.equalTo(titleLabel.snp.bottom).offset(4) make.top.equalTo(titleLabel.snp.bottom).offset(4)
} }
vSeparatorLine.snp.remakeConstraints { make in vSeparatorLine.snp.remakeConstraints { make in
make.left.equalTo(companyLabel.snp.right).offset(marginBetweenVLine) make.left.equalTo(companyLabel.snp.right).offset(YHResourceTableViewCell.marginBetweenVLine)
make.centerY.equalTo(companyLabel) make.centerY.equalTo(companyLabel)
make.width.equalTo(1) make.width.equalTo(1)
make.height.equalTo(8) make.height.equalTo(8)
...@@ -215,7 +221,7 @@ private extension YHResourceTableViewCell { ...@@ -215,7 +221,7 @@ private extension YHResourceTableViewCell {
// 行业标签约束 // 行业标签约束
industryLabel.snp.remakeConstraints { make in industryLabel.snp.remakeConstraints { make in
make.left.equalTo(vSeparatorLine.snp.right).offset(marginBetweenVLine) make.left.equalTo(vSeparatorLine.snp.right).offset(YHResourceTableViewCell.marginBetweenVLine)
make.right.lessThanOrEqualTo(rightArrow.snp.left) make.right.lessThanOrEqualTo(rightArrow.snp.left)
make.top.equalTo(companyLabel) make.top.equalTo(companyLabel)
make.bottom.equalToSuperview().offset(-32) make.bottom.equalToSuperview().offset(-32)
...@@ -236,7 +242,7 @@ private extension YHResourceTableViewCell { ...@@ -236,7 +242,7 @@ private extension YHResourceTableViewCell {
// 行业标签约束 // 行业标签约束
industryLabel.snp.remakeConstraints { make in industryLabel.snp.remakeConstraints { make in
make.left.equalTo(vSeparatorLine.snp.right).offset(marginBetweenVLine) make.left.equalTo(vSeparatorLine.snp.right).offset(YHResourceTableViewCell.marginBetweenVLine)
make.right.lessThanOrEqualTo(rightArrow.snp.left) make.right.lessThanOrEqualTo(rightArrow.snp.left)
make.top.equalTo(companyLabel.snp.bottom).offset(4) make.top.equalTo(companyLabel.snp.bottom).offset(4)
make.bottom.equalToSuperview().offset(-32) make.bottom.equalToSuperview().offset(-32)
......
...@@ -72,9 +72,6 @@ class YHResourceViewModel: NSObject { ...@@ -72,9 +72,6 @@ class YHResourceViewModel: NSObject {
self.arrResourceData?.append(contentsOf: mockData) self.arrResourceData?.append(contentsOf: mockData)
} }
// 计算每个模型的高度参数
self.arrResourceData?.forEach { $0.calHeightParam() }
self.currentPage += 1 self.currentPage += 1
self.hasMoreForResource = self.currentPage <= 5 // 模拟5页数据 self.hasMoreForResource = self.currentPage <= 5 // 模拟5页数据
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment