Ich habe eine xib
für eine UITableViewCell
erstellt, die in bestimmten Fällen Ansichten hinzugefügt werden.UITableCell Höhe ist nicht korrekt
Es muss die Höhe entsprechend der Anzahl der Ansichten, die der Zelle hinzugefügt werden, korrekt angepasst werden.
Was ich getan habe, ist die folgende Die UIImage
und UILabel
eine bottom constraint
haben, die auf die top
des UIView
Der UIView
gesetzt wird, hat eine height constraint
die equal to or greater then 0
ist. Wenn also keine Ansichten zur Zelle hinzugefügt werden, müssen sie 0 sein. Wenn Zellen zur Zelle hinzugefügt werden, müssen sie richtig dimensioniert werden.
Der Code für meine Zelle sieht wie folgt aus
struct MenuTableCellContents {
var identifier: cellIdentifier
var cellImage: UIImage
var cellText: String
//var cellFont: UIFont
var cellTextColor: UIColor
var subCells: [MenuTableCellContents]?
/// Normal cells without subcells
init(identifier: cellIdentifier, cellImage: UIImage, cellText: String, /*cellFont: UIFont,*/ cellTextColor: UIColor) {
self.identifier = identifier
self.cellImage = cellImage
self.cellText = cellText
self.cellTextColor = cellTextColor
//self.cellFont = cellFont
}
/// Cells with subcells
init(identifier: cellIdentifier, cellImage: UIImage, cellText: String, /*cellFont: UIFont,*/ cellTextColor: UIColor, subCells: [MenuTableCellContents]) {
self.identifier = identifier
self.cellImage = cellImage
self.cellText = cellText
self.cellTextColor = cellTextColor
//self.cellFont = cellFont
self.subCells = subCells
}
enum cellIdentifier {
case standard
case noIcon
case subcell
}
}
class MenuTableCell: UITableViewCell {
@IBOutlet var cell_icon: UIImageView!
@IBOutlet var cell_label: UILabel!
@IBOutlet var cell_contentView: UIView!
@IBOutlet var cell_contentView_height: NSLayoutConstraint!
var cellImage: UIImage?
var cellText: String?
var cellTextColor: UIColor?
//var cellFont: UIFont?
var subCells: [MenuTableCellContents]?
private var contentViewHeight: CGFloat?
override func draw(_ rect: CGRect) {
drawSubCells()
cell_label.text = cell_label.text?.uppercased()
super.draw(rect)
}
/// draw the subcells when needed
func drawSubCells() {
// Check if there are subcells
if let cells = subCells {
let count = CGFloat(cells.count)
// Set the height for the subcells
contentViewHeight = 40 * count
let contentViewFrame = CGRect(x: self.frame.width, y: self.cell_contentView.frame.origin.y, width: self.frame.width, height: contentViewHeight!)
cell_contentView?.frame = contentViewFrame
var i = 0
for cell in cells {
// create a view for each cell
let cellView = UIView()
let cellFrame = CGRect(x: 0, y: CGFloat(i * 40), width: contentViewFrame.width, height: 40)
cellView.frame = cellFrame
// add a image view to each cell
let imageView = UIImageView(image: cell.cellImage)
let imageViewFrame = CGRect(x: 0, y: (cellFrame.height - cellFrame.height/2)/2, width: cellFrame.width * 0.2, height: cellFrame.height/2)
imageView.frame = imageViewFrame
imageView.contentMode = .scaleAspectFit
// add a button to each cell
let cellLabel = UILabel()
let cellLabelFrame = CGRect(x: imageViewFrame.width, y: 0, width: cellFrame.width - imageViewFrame.width, height: 40)
cellLabel.frame = cellLabelFrame
//cellLabel.font = cell.cellFont
cellLabel.text = cell.cellText.uppercased()
cellLabel.textColor = cell.cellTextColor
cellLabel.minimumScaleFactor = 15
// add the imageview and button to the subcell view
cellView.addSubview(imageView)
cellView.addSubview(cellLabel)
// add the subcell to the cell
cell_contentView?.addSubview(cellView)
i += 1
}
setupConstraints(height: contentViewHeight!)
}
}
/// Set the height constraint for the cell content view in which the subcells reside
func setupConstraints(height: CGFloat) {
cell_contentView.frame.size.height = height
}
}
dann als meine Tabelle Methoden suchen
class MenuViewController: UIViewController, UITableViewDelegate {
@IBOutlet var menu_table: MenuTableView!
var cellContent: [MenuTableCellContents] = []
override func viewDidLoad() {
super.viewDidLoad()
setupCells()
let menuCell = UINib(nibName: "MenuTableCell", bundle: nil)
menu_table.register(menuCell, forCellReuseIdentifier: "menuCell")
menu_table.delegate = self
menu_table.dataSource = self
menu_table.rowHeight = UITableViewAutomaticDimension
menu_table.estimatedRowHeight = 50
self.revealViewController().frontViewShadowRadius = 0
self.revealViewController().frontViewShadowOffset = CGSize.zero
}
/// setup the required cells for the menu
func setupCells() {
let normal1 = MenuTableCellContents(identifier: .standard, cellImage: UIImage(named: "icn_pin")!, cellText: "normal1", cellTextColor: UIColor.black)
let normal2 = MenuTableCellContents(identifier: .standard, cellImage: UIImage(named: "icn_pin")!, cellText: "normal2", cellTextColor: UIColor.black)
let normal3 = MenuTableCellContents(identifier: .standard, cellImage: UIImage(named: "icn_pin")!, cellText: "normal3", cellTextColor: UIColor.black)
let sub1 = MenuTableCellContents(identifier: .subcell, cellImage: UIImage(named: "icn_pin")!, cellText: "sub1", cellTextColor: UIColor.black)
let sub2 = MenuTableCellContents(identifier: .subcell, cellImage: UIImage(named: "icn_pin")!, cellText: "sub2", cellTextColor: UIColor.black)
let sub3 = MenuTableCellContents(identifier: .subcell, cellImage: UIImage(named: "icn_pin")!, cellText: "sub3", cellTextColor: UIColor.black)
let subs = MenuTableCellContents(identifier: .standard, cellImage: UIImage(named: "icn_pin")!, cellText: "subs", cellTextColor: UIColor.black, subCells: [sub1, sub2, sub3])
let normal4 = MenuTableCellContents(identifier: .standard, cellImage: UIImage(named: "icn_pin")!, cellText: "normal4", cellTextColor: UIColor.black)
cellContent = [normal1, normal2, normal3, subs, normal4]
}
}
/// required methods for the uitableview
extension MenuViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellContent.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let content = cellContent[indexPath.item]
let cell = menu_table.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as! MenuTableCell
cell.cell_icon.image = content.cellImage
cell.cell_label.text = content.cellText
cell.cell_label.textColor = content.cellTextColor
//cell.cell_label.font = content.cellFont
if let subCells = content.subCells {
cell.subCells = subCells
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
}
folgt
weiß jemand, wo ich falsch gehe? Die Größe wird nicht korrekt angepasst, wenn die Ansichten hinzugefügt werden. Die Höhe bleibt Null.
UPDATE
ich ein Testprojekt mit dem Thema GitHub hochgeladen haben. https://github.com/MaikoHermans/tableCellDynamicHeightProblem
UPDATE 2
Für alle vor dem gleichen Problem ich das GitHub Projekt in Update 1 auf die Arbeits erwähnt aktualisiert haben.
Ihr Etikett wachsen? oder seine Höhe ist festgelegt ?, wenn Sie behoben sind, können Sie Ihre Zellenhöhe als 40 * Anzahl der Unteransichten + Label Höhe berechnen –