表格视图中的行不会扩展它的宽度并且是堆叠的 Swift
Posted
技术标签:
【中文标题】表格视图中的行不会扩展它的宽度并且是堆叠的 Swift【英文标题】:Rows in table view do not expand the width of it and are stacked Swift 【发布时间】:2019-10-30 22:16:15 【问题描述】:我花了一个小时试图弄清楚我做错了什么,但没有成功。下面是代码和结果的图像。在表格的第一行中,所有行都在另一个之上出现。并且该行不会像我在约束中设置的那样扩展表格的宽度。我究竟做错了什么?谢谢你。 表视图类:
class TableViewListType: UITableView
override init(frame: CGRect, style: UITableView.Style)
super.init(frame: frame, style: style)
translatesAutoresizingMaskIntoConstraints = false
allowsSelection = true
allowsMultipleSelection = false
allowsSelectionDuringEditing = true
allowsMultipleSelectionDuringEditing = true
dragInteractionEnabled = false
backgroundColor = .clear
separatorColor = .white
separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
indicatorStyle = .white
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
表格视图的行类:
class SuperRowForProfileAttributesTable: UITableViewCell
//MARK: - Properties.
internal let firstLabel: LabelForRowInList =
let label = LabelForRowInList(frame: .zero)
return label
()
internal let secondLabel: LabelForRowInList =
let label = LabelForRowInList(frame: .zero)
return label
()
internal let thirdLabel: LabelForRowInList =
let label = LabelForRowInList(frame: .zero)
return label
()
internal let fourthLabel: LabelForRowInList =
let label = LabelForRowInList(frame: .zero)
return label
()
//MARK: - Init.
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
super.init(style: style, reuseIdentifier: reuseIdentifier)
translatesAutoresizingMaskIntoConstraints = false
clipsToBounds = true
backgroundColor = .clear
setupViews()
required init?(coder: NSCoder)
fatalError("init(coder:) has not been implemented")
//MARK: - Functions.
internal func setupViews()
addSubview(firstLabel)
addSubview(secondLabel)
addSubview(thirdLabel)
addSubview(fourthLabel)
let firstConstraints = [
firstLabel.topAnchor.constraint(equalTo: topAnchor),
firstLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
firstLabel.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 1/3),
firstLabel.widthAnchor.constraint(equalTo: widthAnchor)
]
NSLayoutConstraint.activate(firstConstraints)
let secondConstraints = [
secondLabel.topAnchor.constraint(equalTo: firstLabel.bottomAnchor),
secondLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
secondLabel.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 1/3),
secondLabel.widthAnchor.constraint(equalTo: widthAnchor)
]
NSLayoutConstraint.activate(secondConstraints)
let thirdConstraints = [
thirdLabel.topAnchor.constraint(equalTo: secondLabel.bottomAnchor),
thirdLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
thirdLabel.trailingAnchor.constraint(equalTo: centerXAnchor),
thirdLabel.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 1/3),
]
NSLayoutConstraint.activate(thirdConstraints)
let fourthConstraints = [
fourthLabel.topAnchor.constraint(equalTo: secondLabel.bottomAnchor),
fourthLabel.leadingAnchor.constraint(equalTo: centerXAnchor),
fourthLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
fourthLabel.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 1/3)
]
NSLayoutConstraint.activate(fourthConstraints)
class RowForExperienceInProfileTable: SuperRowForProfileAttributesTable
//MARK: - Properties.
internal var valueForExperienceRow: ExperienceModelForProfileAttributes!
didSet
firstLabel.text = valueForExperienceRow.jobTitle
secondLabel.text = valueForExperienceRow.companyName
thirdLabel.text = valueForExperienceRow.startedWork
fourthLabel.text = valueForExperienceRow.finishedWork
class RowForSkillInProfileTable: SuperRowForProfileAttributesTable
//MARK: - Properties.
internal var valueForSkillRow: SkillsModelForProfileAttributes!
didSet
firstLabel.text = valueForSkillRow.skill
//MARK: - Init.
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
required init?(coder: NSCoder)
fatalError("init(coder:) has not been implemented")
override func setupViews()
addSubview(firstLabel)
let firstConstraints = [
firstLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
firstLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
firstLabel.widthAnchor.constraint(equalTo: widthAnchor, constant: 0),
firstLabel.heightAnchor.constraint(equalTo: heightAnchor, constant: 0)
]
NSLayoutConstraint.activate(firstConstraints)
class RowForEducationInProfileTable: SuperRowForProfileAttributesTable
//MARK: - Properties.
internal var valueForEducationRow: EducationModelForProfileAttributes!
didSet
firstLabel.text = valueForEducationRow.institutionName
secondLabel.text = valueForEducationRow.degreeName
thirdLabel.text = valueForEducationRow.startedStudy
fourthLabel.text = valueForEducationRow.finishedStudy
VC:
fileprivate var experienceForProfile = [ExperienceModelForProfileAttributes(jobTitle: "Tester", companyName: "Testing Company", startedWork: "May 2019", finishedWork: "October 2019"), ExperienceModelForProfileAttributes(jobTitle: "Welder", companyName: "Welding Company", startedWork: "January 2018", finishedWork: "May 2020")]
fileprivate var skillsForProfile = [SkillsModelForProfileAttributes]()
fileprivate var educationForProfile = [EducationModelForProfileAttributes]()
fileprivate lazy var tableForAttributes: TableViewListType =
let table = TableViewListType(frame: .zero, style: .plain)
table.delegate = self
table.dataSource = self
return table
()
override func viewDidLoad()
super.viewDidLoad()
navigationController?.navigationBar.isHidden = true
tableForAttributes.register(RowForExperienceInProfileTable.self, forCellReuseIdentifier: firstIDForTable)
tableForAttributes.register(RowForSkillInProfileTable.self, forCellReuseIdentifier: secondIDForTable)
tableForAttributes.register(RowForEducationInProfileTable.self, forCellReuseIdentifier: thirdIDForTable)
func numberOfSections(in tableView: UITableView) -> Int
return 1
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
var numberOfRows = 0
switch selectedMimicIndex
case 0: numberOfRows = experienceForProfile.count
case 1: numberOfRows = skillsForProfile.count
case 2: numberOfRows = educationForProfile.count
default: print("nu such rows for attributes table in own profile")
return numberOfRows
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
switch selectedMimicIndex
case 0: let cell = tableView.dequeueReusableCell(withIdentifier: firstIDForTable, for: indexPath) as! RowForExperienceInProfileTable
cell.valueForExperienceRow = experienceForProfile[indexPath.row]
return cell
case 1: let cell = tableView.dequeueReusableCell(withIdentifier: secondIDForTable, for: indexPath) as! RowForSkillInProfileTable
cell.valueForSkillRow = skillsForProfile[indexPath.row]
return cell
case 2: let cell = tableView.dequeueReusableCell(withIdentifier: thirdIDForTable, for: indexPath) as! RowForEducationInProfileTable
cell.valueForEducationRow = educationForProfile[indexPath.row]
return cell
default: return UITableViewCell()
selectedMimicIndex 值改变了我拥有的集合视图的 didSelectItem() 函数;一个 cv 控制表格显示的单元格。当用户更改选定的 cv 单元格时,在更改 Int 值后,此处调用 reloadData()。 还要注意第二行比它应该高得多;忽略我指定的高度。
【问题讨论】:
【参考方案1】:对于您需要设置的每个标签并将其添加到 contentView
firstLabel.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(firstLabel)
也是单元格底部的最底部标签
fourthLabel.bottomAnchor.constraint(equalTo:self.contentView.bottomAnchor, constant:-20)
【讨论】:
是的,这对我来说很清楚。谢谢楼主以上是关于表格视图中的行不会扩展它的宽度并且是堆叠的 Swift的主要内容,如果未能解决你的问题,请参考以下文章