Self sizing tableview inside self sizing tableview cell

后端 未结 3 2032
庸人自扰
庸人自扰 2020-12-22 07:44

Let\'s say I have hierarchy like this:

*TableViewCell
**TableView
***TableViewCell

and all of them should be resizable. Did someone face th

相关标签:
3条回答
  • 2020-12-22 08:42

    This is actually not an answer to the question, but just an explanation.
    (Wrote here because of the character count limitation for the comments).

    The thing is that you're trying to insert a vertically scrollable view inside another vertically scrollable view. If you don't disable the nested tableview's scroll ability, you will have a glitch while scrolling, because the system wouldn't know to whom pass the scroll event (to the nested tableview, or to the parent tableview). So in our case, you'll have to disable the "scrollable" property for the nested tableviews, hence you'll have to set the height of the nested tableview to be equal to its content size. But this way you will lose the advantages of tableview (i.e. cell reusing advantage) and it will be the same as using an actual UIScrollView. But, on the other hand, as you'll have to set the height to be equal to its content size, then there is no reason to use UIScrollView at all, you can add your nested cells to a UIStackView, and you tableview will have this hierarchy:

    *TableView
    **TableViewCell
    ***StackView
    ****Items
    ****Items
    ****Items
    ****Items
    

    But again, the right solution is using multi-sectional tableview. Let your cells be section headers of the tableview, and let inner cells be the rows of the tableview.

    0 讨论(0)
  • 2020-12-22 08:45
    • Use xib files to simplify the hierarchy.
    • Get a tableView on your storyboard, and create a nib file for your tableViewCell(say CustomTableViewCell). Inside it create a tableView and again create one more tableViewCell xib file. Now, no need of setting labels into your xib file,(if you want only labels in cells and nothing else, if not, there is another way of adding constraints)
    • Say you have an array of text, some strings are long and some are short.
    • register nib file in CustomTableViewCell and extend it to use Delegate and DataSource.
    • register this CustomTableViewCell in ViewController.
    • While declaring a cell in CustomTableViewCell, just do=
    • cell.textLabel?.text = content
    • cell.textLabel?.numberOfLines = 0
    • Use heightForRowAt to set outer tableViewCell's height, and let the inner tableView to scroll inside.
    0 讨论(0)
  • 2020-12-22 08:49

    here is an example of how to make a tableview inside a table view cell with automatic height for the cells.

    You should use the 'ContentSizedTableView' class for the inner tableViews.

    class ViewController: UIViewController {
    
        @IBOutlet weak var outerTableView: UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            outerTableView.rowHeight = UITableView.automaticDimension
            outerTableView.estimatedRowHeight = UITableView.automaticDimension
            outerTableView.delegate = self
            outerTableView.dataSource = self
        }
    
    }
    
    final class ContentSizedTableView: UITableView {
        override var contentSize:CGSize {
            didSet {
                invalidateIntrinsicContentSize()
            }
        }
    
        override var intrinsicContentSize: CGSize {
            layoutIfNeeded()
            sizeToFit()
            return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
        }
    }
    
    extension ViewController: UITableViewDelegate, UITableViewDataSource {
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
            return 10
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableTableViewCell
    
            return cell!
        }
    
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableView.automaticDimension
        }
    }
    
    0 讨论(0)
提交回复
热议问题