TableView Height as Per Content Size in Swift5 : iOS

TableView Height as Per Content Size in Swift5 : iOS
April 3, 2022 No Comments Development Pushpendra Kumar

Hey, Guys I hope you are looking for the height adjustment of the tableview in swift. If Yes, They you are on the right place. In this tutorial, I have given the complete and very easy demonstration about the UITableviewHeight adjustment of the as per content size. And while you are going through with this process check carefully every point. Check the demonstration till the end. I hope you will get the answer of your question.  Video Link

Overview

able views in iOS display rows of vertically scrolling content in a single column. Each row in the table contains one piece of your app’s content. You can configure a table to display a single long list of rows, or you can group related rows into sections to make navigating the content easier. More information

TableView Height

The relationship between two user interface objects that must be satisfied by the constraint-based layout system. Read more on Apple Developer Docs

//
//  ViewController.swift
//  TableView
//
//  Created by Pushpendra  Kumar on 03/04/22.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var tableViewHeight: NSLayoutConstraint!
    
    private var itemList : [String] = [String]()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setTableView()
    }

    private func setTableView(){
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "TableViewCell")
        
        self.setTabeleViewData()
    }
    
    override func viewWillLayoutSubviews() {
        self.tableViewHeight.constant = self.tableView.contentSize.height
    }
    
    private func setTabeleViewData(){
        for i in 0...10 {
            self.itemList.append("Some content at the particular index \(i + 1)")
        }
        self.tableView.reloadData()
        self.tableView.invalidateIntrinsicContentSize()
    }
}
extension ViewController : UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.itemList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
        cell.lbDescription.text = self.itemList[indexPath.row]
        return cell
    }
}

Complete code for demo. And also watch the complete video for more and deep understanding.

Learn more on wepstech.com

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *