قالب وردپرس درنا توس
Home / IOS Development / ios – Table size self-resizing table view

ios – Table size self-resizing table view



Let's say I have hierarchy like this:

  * TableViewCell
** Tableview
*** TableViewCell

and all of them can be changed. Did anyone face this type of problem? Previously, I have used many temporary solutions such as systemLayoutSizeFitting or pre-calculation of height in heightForRowAt but it always breaks some constraints because TableViewCell has height limit corresponding to the estimated height and some kind of magic behavior appears. Some ways to do this live?

Current solution:

  class SportCenterReviewsTableCell: UITableViewCell, MVVMView {
var tableView: SelfSizedTableView = {
let view = SelfSizedTableView (frame: .zero)
view.clipsToBounds = true
view.tableFooterView = UIView ()
view.separatorStyle =. no
view.isScrollEnabled = false
view.showsVerticalScrollIndicator = false
view.estimatedRowHeight = 0
if # available (iOS 1
1.0, *) { view.contentInsetAdjustmentBehavior = .never } other { // Fallback on previous versions } return view } () private func mark () { contentView.addSubview (Tableview) tableView.delegate = self tableView.dataSource = self tableView.register (ReviewsTableViewCell.self, forCellReuseIdentifier: "Cell") tableView.snp.makeConstraints () {layer make.top.equalTo (seeAllButton.snp.bottom) .offset (12) make.left.equalTo (contentView.snp.left) make.right.equalTo (contentView.snp.right) make.bottom.lessThanOrEqualTo (contentView.snp.bottom) } } func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { la cell = tableView.dequeueReusableCell (withIdentifier: "Cell", for: indexPath) as! Review Stable View Cell cell.viewModel = viewModel.cellViewModels [indexPath.row] return cell } func tableView (_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { la cell = tableView.dequeueReusableCell (withIdentifier: "Cell") as! Review Stable View Cell cell.viewModel = viewModel.cellViewModels [indexPath.row] cell.setNeedsLayout () cell.layoutIfNeeded () la size = cell.contentView.systemLayoutSizeFitting (UIView.layoutFittingCompressionSize, withHorizontalFittingPriority: .defaultHigh, verticalFittingPriority: .defaultLow) return size. height } }

Self-Size TableView Class:

  Class SelfSizedTableView: UITableView {
override func reloadData () {
super.reloadData ()
self.invalidateIntrinsicContentSize ()
self.layoutIfNeeded ()
}

override was intrinsicContentSize: CGSize {
self.setNeedsLayout ()
self.layoutIfNeeded ()
return size
}
}


Source link