我知道有很多类似的问题。但是,我的问题没有一个正确的答案。 我需要以编程方式创建自定义UITableView - 根本不使用拖放。只是快速的代码。 我已经做了几乎所有的事情,除了一件事没有用。
Custom TableView Cell看起来像这样
---------------------------------
-------
| Pic | Name
| | Profession
-------
Multi-line text here (title)
Multi-line text here again that
will have more description of
what is written above
List of all phone numbers,
multi-line again - multiple phone
numbers
footer
----------------------------------
除了自动调整表格单元格高度不起作用外,我已经成功了。 我想以编程方式完成此任务,因此面临问题。另外,我在iOS编程方面没有太多经验。
这是我在阅读了许多stackoverflow答案和其他来源后得出的模式。
模型
class Model {
// members
init(..) {
// members initialized
}
}
CustomCell
class CustomCell: UITableViewCell {
// cell members, fields
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
// all fields initialized
}
override func prepareForReuse() {
super.prepareForReuse()
}
override func layoutSubviews() {
super.layoutSubviews()
self.updateConstraints()
}
func autoAdjustTextView(textView: UITextView) {
textView.scrollEnabled = false // this is important
let fixedWidth = textView.frame.size.width
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
var newFrame = textView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
textView.frame = newFrame
}
override func updateConstraints() {
super.updateConstraints()
// set AutoLayout constraints using Cartography
autoAdjustTextView(descriptionMultiLineText)
autoAdjustTextView(titleMultiLineText)
autoAdjustTextView(phoneNumberMultiLineText)
constrain(background) { background in
background.top == background.superview!.top
background.left == background.superview!.left
background.right == background.superview!.right
// background.height == CELL_HEIGHT
}
constrain(background, iconContainerView) { background, iconContainerView in
iconContainerView.left == background.left + CustomSizes.PADDING
iconContainerView.top == background.top + CustomSizes.PADDING
}
constrain(background, textIconLabel, userNameLabel) { background, imageLabel, nameLabel in
nameLabel.left == imageLabel.right + CustomSizes.PADDING
nameLabel.top == imageLabel.top
}
constrain(background, userNameLabel, professionLabel) { background, nameLabel, label in
label.left == nameLabel.left
label.top == nameLabel.bottom + CustomSizes.PADDING_SMALL
}
constrain(background, professionLabel, updatedAtLabel) { background, professionLabel, label in
label.left == professionLabel.right + CustomSizes.PADDING
label.top == professionLabel.top
}
constrain(titleMultiLineText, background, professionLabel) { titleMultiLineText, background, professionLabel in
titleMultiLineText.left == background.left + CustomSizes.PADDING
titleMultiLineText.right == background.right - CustomSizes.PADDING
titleMultiLineText.top == professionLabel.bottom + CustomSizes.PADDING_SMALL
}
constrain(descriptionMultiLineText, background, titleMultiLineText) { descriptionMultiLineText, background, titleMultiLineText in
descriptionMultiLineText.left == background.left + CustomSizes.PADDING
descriptionMultiLineText.right == background.right - CustomSizes.PADDING
descriptionMultiLineText.top == titleMultiLineText.bottom
}
constrain(phoneNumberMultiLineText, background, descriptionMultiLineText) { phoneNumberMultiLineText, background, descriptionMultiLineText in
phoneNumberMultiLineText.left == background.left + CustomSizes.PADDING
phoneNumberMultiLineText.right == background.right - CustomSizes.PADDING
phoneNumberMultiLineText.top == descriptionMultiLineText.bottom
}
// more constraints for some icons on a single line below the phone number multi line field
didSetupConstraints = true
}
}
的ViewController
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// members and variables
init() {
super.init(nibName: nil, bundle: nil)
print("init()")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
// show loading
self.setUpView(true)
}
func setUpView(isFirstTime: Bool) {
// load data from network
if isFirstTime {
self.setupTableView()
self.addConstraints()
} else {
self.tableView.reloadData()
}
}
func setupTableView() {
self.tableView = UITableView(frame: view.bounds, style: .Grouped)
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = .None
tableView.registerClass(CustomCell.self, forCellReuseIdentifier: NSStringFromClass(CustomCell))
tableView.scrollEnabled = true
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension
self.view.addSubview(tableView)
}
func addConstraintsForView() {
constrain(self.view, tableView) { view, tableView in
tableView.top == view.top
tableView.left == view.left
tableView.right == view.right
tableView.bottom == view.bottom
}
}
// other tableView methods implemented
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.All
}
}
我现在该怎么办? 或者有更好的方法吗?