Cell内的TableView动态高度问题
Posted
技术标签:
【中文标题】Cell内的TableView动态高度问题【英文标题】:TableView dynamic height problem inside a Cell 【发布时间】:2018-09-26 08:25:10 【问题描述】:我在 tableViewCell 中有一个动态高度的 tableView 以及其他具有动态高度和与单元格成比例宽度的 ui 组件。问题:渲染和滚动外部 uitableview 时,内部 tableViewCells 会缩小。这是代码:
import UIKit
import WebKit
import SVProgressHUD
class DashboardProgramcell: UITableViewCell, UITableViewDataSource, UITableViewDelegate
数据,模型在这里..
public var icerikModel:WMPIcerikModel?
static let ID:String = "DashboardProgramcell"
static let turCellPair = (tur: TurID.Program, cellId: ID)
var kListData: WMPArrayOfKatilimciModel?
单元子视图..
//MainContainer
var container: UIView =
var container = UIView()
container.translatesAutoresizingMaskIntoConstraints = false
return container
()
//Left side
var leftContainer: UIView =
var leftContainer = UIView()
leftContainer.translatesAutoresizingMaskIntoConstraints = false
return leftContainer
()
var gSaat: UILabel =
var gSaat = UILabel()
gSaat.translatesAutoresizingMaskIntoConstraints = false
gSaat.numberOfLines = 0
gSaat.font = UIFont.systemFont(ofSize: 11)
return gSaat
()
var sure: UILabel =
var sure = UILabel()
sure.translatesAutoresizingMaskIntoConstraints = false
sure.numberOfLines = 0
sure.backgroundColor = ColorHelper.mercury
sure.textAlignment = .center
sure.font = UIFont.systemFont(ofSize: 11)
return sure
()
var ptad: UITextView =
var ptad = UITextView()
ptad.textColor = .white
ptad.textAlignment = .center
ptad.isUserInteractionEnabled = false
ptad.isScrollEnabled = false
ptad.translatesAutoresizingMaskIntoConstraints = false
ptad.font = UIFont.systemFont(ofSize: 11)
ptad.textContainerInset = UIEdgeInsets.zero
ptad.textContainer.lineFragmentPadding = 0
return ptad
()
//Right part
var rightContainer: UIView =
var rightContainer = UIView()
rightContainer.translatesAutoresizingMaskIntoConstraints = false
return rightContainer
()
var rightTopTextView: UILabel =
var rightTopTextView = UILabel()
rightTopTextView.translatesAutoresizingMaskIntoConstraints = false
rightTopTextView.numberOfLines = 0
rightTopTextView.lineBreakMode = NSLineBreakMode.byWordWrapping
rightTopTextView.font = UIFont.systemFont(ofSize: 12)
rightTopTextView.backgroundColor = ColorHelper.arzPopup
return rightTopTextView
()
这是内部 tableView 及其约束!
var kList: UITableView =
var kList = UITableView()
kList.contentMode = .scaleAspectFit
kList.bounces = false
kList.bouncesZoom = false
kList.alwaysBounceVertical = false
kList.translatesAutoresizingMaskIntoConstraints = false
kList.tag = KListCell.TAG
kList.rowHeight = UITableView.automaticDimension
kList.estimatedRowHeight = UITableView.automaticDimension
kList.register(KListCell.self, forCellReuseIdentifier: KListCell.ID)
kList.tableFooterView = UIView(frame: .zero)
return kList
()
var kListHeightConstraint:NSLayoutConstraint?
var rTopTextViewHeightConstraint:NSLayoutConstraint?
构造函数和约束激活!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.tag = TurID.Program.rawValue
self.selectionStyle = .none
self.addSubview(container)
container.addSubview(leftContainer) //OK
container.addSubview(rightContainer) //OK
leftContainer.addSubview(gSaat) //OK
leftContainer.addSubview(sure) //OK
leftContainer.addSubview(ptad)
rightContainer.addSubview(rightTopTextView)
rightContainer.addSubview(kList)
NSLayoutConstraint.activate([
container.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 2),
container.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -2),
container.topAnchor.constraint(equalTo: self.topAnchor, constant: 2),
container.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -2),
leftContainer.topAnchor.constraint(equalTo: container.topAnchor),
leftContainer.leftAnchor.constraint(equalTo: container.leftAnchor),
leftContainer.bottomAnchor.constraint(equalTo: container.bottomAnchor),
leftContainer.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 0.30),
leftContainer.heightAnchor.constraint(equalTo: container.heightAnchor),
leftContainer.rightAnchor.constraint(equalTo: rightContainer.leftAnchor),
rightContainer.topAnchor.constraint(equalTo: container.topAnchor),
rightContainer.bottomAnchor.constraint(equalTo: container.bottomAnchor),
rightContainer.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 0.68),
rightContainer.heightAnchor.constraint(equalTo: container.heightAnchor),
rightContainer.rightAnchor.constraint(equalTo: container.rightAnchor),
gSaat.topAnchor.constraint(equalTo: leftContainer.topAnchor, constant: 4),
gSaat.leftAnchor.constraint(equalTo: leftContainer.leftAnchor, constant: 4),
gSaat.rightAnchor.constraint(equalTo: leftContainer.rightAnchor, constant: -4),
gSaat.bottomAnchor.constraint(equalTo: sure.topAnchor, constant: -2),
sure.leftAnchor.constraint(equalTo: leftContainer.leftAnchor, constant: 4),
sure.rightAnchor.constraint(equalTo: leftContainer.rightAnchor, constant: -4),
sure.bottomAnchor.constraint(equalTo: ptad.topAnchor, constant:-2),
ptad.leftAnchor.constraint(equalTo: leftContainer.leftAnchor, constant: 4),
ptad.rightAnchor.constraint(equalTo: leftContainer.rightAnchor),
ptad.bottomAnchor.constraint(lessThanOrEqualTo: leftContainer.bottomAnchor),
ptad.heightAnchor.constraint(equalToConstant: 16),
rightTopTextView.topAnchor.constraint(equalTo: rightContainer.topAnchor, constant:4),
rightTopTextView.leftAnchor.constraint(equalTo: rightContainer.leftAnchor, constant:4),
rightTopTextView.rightAnchor.constraint(equalTo: rightContainer.rightAnchor, constant:-4),
rightTopTextView.bottomAnchor.constraint(equalTo: kList.topAnchor),
kList.leftAnchor.constraint(equalTo: rightContainer.leftAnchor, constant:2),
kList.rightAnchor.constraint(equalTo: rightContainer.rightAnchor, constant:-2),
kList.bottomAnchor.constraint(equalTo: rightContainer.bottomAnchor, constant:-2)
])
container.addBorders(edges: [.left, .right, .bottom], color: ColorHelper.programBg, width: 1)
kList.dataSource = self
kList.delegate = self
// kListHeightConstraint = kList.heightAnchor.constraint(equalToConstant: 150)
// kListHeightConstraint?.isActive = true
// rTopTextViewHeightConstraint = rightTopTextView.heightAnchor.constraint(equalToConstant: 120)
// rTopTextViewHeightConstraint?.isActive = true
self.rightTopTextView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapItself)))
self.layoutIfNeeded()
@objc func tapItself()
SVProgressHUD.show()
DispatchQueue.global(qos: .background).async
SoapService(vc: self.viewControllerForTableView!).KayitDetay(tur: self.icerikModel!.TID!, kayitID: self.icerikModel!.ID!)
override func layoutSubviews()
super.layoutSubviews()
public func adjustSubViews()
DispatchQueue.main.async
if let icerik = self.icerikModel
//Left
self.gSaat.text = icerik.GSaat ?? ""
self.sure.text = icerik.Sure ?? ""
self.ptad.text = icerik.PTAD ?? ""
self.ptad.backgroundColor = self.ptad.text.isEmpty ? UIColor.clear : (self.ptad.text.hasPrefix("Arz") || self.ptad.text.hasPrefix("arz") ? TurID.Arz.tableviewBg() : TurID.Ziyaret.tableviewBg())
//Right
if let ack = icerik.Ack
self.rightTopTextView.attributedText = ack.converthtml().convertHtmlAttributed()
// self.rTopTextViewHeightConstraint?.constant = self.rightTopTextView.contentSize.height
else
self.rightTopTextView.heightAnchor.constraint(equalToConstant: 0).isActive = true
if let KListe = icerik.KListe, KListe.count != 0
self.kListData = KListe
self.kList.reloadData()
else
// self.kListHeightConstraint?.constant = 5
SVProgressHUD.dismissMainUI
self.layoutIfNeeded()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
if let _kListData = self.kListData, _kListData.count != 0
return _kListData.count
return 0
var kTVHeight:CGFloat = 0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let kListCell = tableView.dequeueReusableCell(withIdentifier: KListCell.ID) as! KListCell
if let _kListData = self.kListData , _kListData.count != 0
kListCell.katilimciModel = _kListData[indexPath.row]
kListCell.adjustSubViews()
// self.kTVHeight += kListCell.frame.height
if indexPath.row == _kListData.count - 1 && kTVHeight > 10
// kListHeightConstraint?.constant = kTVHeight
// rTopTextViewHeightConstraint?.constant = rightTopTextView.contentSize.height
// kTVHeight = 0
return kListCell
//If we got cell to display this func will work!
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
if tableView.visibleCells.count == 0
SVProgressHUD.dismissMainUI
//class DashboardProgramcell
class KListCell: UITableViewCell
var katilimciModel:WMPKatilimciModel?
static let ID:String = "KListCell"
static let TAG = 4001
//MainContainer
var container: UIView =
var container = UIView()
container.translatesAutoresizingMaskIntoConstraints = false
return container
()
//Left side
var leftContainer: UIView =
var leftContainer = UIView()
leftContainer.translatesAutoresizingMaskIntoConstraints = false
return leftContainer
()
//Right part
var rightContainer: UIView =
var rightContainer = UIView()
rightContainer.translatesAutoresizingMaskIntoConstraints = false
return rightContainer
()
var adSoyad: UILabel =
var adSoyad = UILabel()
adSoyad.translatesAutoresizingMaskIntoConstraints = false
adSoyad.numberOfLines = 0
adSoyad.font = UIFont.systemFont(ofSize: 10)
return adSoyad
()
var unvan: UILabel =
var unvan = UILabel()
unvan.translatesAutoresizingMaskIntoConstraints = false
unvan.numberOfLines = 0
unvan.font = UIFont.systemFont(ofSize: 10)
return unvan
()
var userImage: UIImageView =
var userImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 12, height: 12))
userImage.translatesAutoresizingMaskIntoConstraints = false
return userImage
()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(container)
container.addSubview(leftContainer) //OK
container.addSubview(rightContainer) //OK
leftContainer.addSubview(userImage) //OK
rightContainer.addSubview(adSoyad)
rightContainer.addSubview(unvan)
self.applyConstraints()
override func layoutSubviews()
super.layoutSubviews()
func applyConstraints()
NSLayoutConstraint.activate([
container.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 2),
container.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -2),
container.topAnchor.constraint(equalTo: self.topAnchor, constant: 2),
container.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -2),
leftContainer.topAnchor.constraint(equalTo: container.topAnchor),
leftContainer.leftAnchor.constraint(equalTo: container.leftAnchor),
leftContainer.bottomAnchor.constraint(equalTo: container.bottomAnchor),
leftContainer.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 0.12),
leftContainer.heightAnchor.constraint(equalTo: container.heightAnchor),
leftContainer.rightAnchor.constraint(equalTo: rightContainer.leftAnchor),
rightContainer.topAnchor.constraint(equalTo: container.topAnchor),
rightContainer.bottomAnchor.constraint(equalTo: container.bottomAnchor),
rightContainer.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 0.86),
rightContainer.heightAnchor.constraint(equalTo: container.heightAnchor),
rightContainer.rightAnchor.constraint(equalTo: container.rightAnchor),
userImage.topAnchor.constraint(equalTo: leftContainer.topAnchor, constant:2),
userImage.heightAnchor.constraint(equalToConstant: 12),
userImage.widthAnchor.constraint(equalToConstant: 12),
userImage.centerXAnchor.constraint(equalTo: leftContainer.centerXAnchor),
userImage.bottomAnchor.constraint(lessThanOrEqualTo: leftContainer.bottomAnchor),
adSoyad.topAnchor.constraint(equalTo: rightContainer.topAnchor, constant: 2),
adSoyad.leftAnchor.constraint(equalTo: rightContainer.leftAnchor, constant: 2),
adSoyad.rightAnchor.constraint(equalTo: rightContainer.rightAnchor, constant: -2),
adSoyad.bottomAnchor.constraint(equalTo: unvan.topAnchor),
unvan.leftAnchor.constraint(equalTo: rightContainer.leftAnchor, constant: 2),
unvan.rightAnchor.constraint(equalTo: rightContainer.rightAnchor, constant: -2),
unvan.bottomAnchor.constraint(equalTo: rightContainer.bottomAnchor, constant: -2),
])
public func adjustSubViews ()
DispatchQueue.main.async
if let katilimci = self.katilimciModel
self.adSoyad.text = katilimci.AD ?? ""
self.unvan.text = katilimci.Ack ?? ""
self.userImage.image = UIImage(named:"userBlack")
【问题讨论】:
这么多代码...您不能在问题仍然存在的情况下删除尽可能多的代码并发布该代码吗? 谢谢,我做了一些修剪。 为什么要嵌套tableview?你想要什么? 我需要显示 1 个 tableView 列,其中每个单元格都包含一些标签、uiview 和 1 个 tableview,其中包含有关该单元格的信息列表。这几乎无法解释我在上面的代码中真正想要什么。 @gomozor 如果您发布UI表单模拟器或设备的屏幕截图会更好。可能你不需要嵌套表格视图只是另一个自定义单元格 【参考方案1】:首先更改此代码:
self.addSubview(container)
到
contentView.addSubview(container)
单元格中的视图应该添加到 contentView,因为它是根布局。
【讨论】:
谢谢。这可能在其他部分有用,但不适用于我的代码。以上是关于Cell内的TableView动态高度问题的主要内容,如果未能解决你的问题,请参考以下文章
iOS知识学习_iOS动态改变TableView Cell高度
如何在tableview Cell内设置tableView的自动高度?
动态增加 UILabel & TableView Cell 的高度?