简体   繁体   中英

Swift4 tableview cell height not dynamic and always static

Please really I need help on this issue it has taken 6 days from me, and I can not find a solution.

I want to make simple table view but its cells always have static height not changed upon items inside it

The result always like this image from emulator .

I want to show this in every cell as example:

1 1 11 1 1111 1 1 11 11 11 1 1 1 11 1 1 11 11 1 11 1 11 11 11

22 22 2 2 2 2 2 2 2 2 2 2 2 22 22 22 22 2 2 22 2 2 2 2 22 2 2 22

33 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 33 3 3 3 33 33 3 3 3 3 3 333 3 3 3 3

444 44 4 44 444 4 4 4 4 4 4 4 4 4 4 44 4 4 44 4 4 4 4 4 4 4 44 44 4

it must be shown in 4 labels in vertical-align but result as you see before

I tried all ways like making view using storyboard or from code but finally, I used library called Stevia but still same result

This is view controller

    class AttendanceControlPage:UIViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var dataTableView: UITableView!
    @IBOutlet weak var navLeftButton: UIBarButtonItem!


    //============================ Base Functions =================//
    override func viewDidLoad() {
        super.viewDidLoad()

        // Some tutorials said to put this two lines but not changes
        dataTableView.estimatedRowHeight = 44
        dataTableView.rowHeight = UITableViewAutomaticDimension


        initPage()

        dataTableView.register(AttendanceControlCell.self, forCellReuseIdentifier: "cellCon")

    }


    //============================ Init Functions =================//

    func initPage() {
            startLoading()
            self.navLeftButton.image = UIImage(named: "icn_drawer")
    }



    //==================== Nav Bar Buttons Actions ===================================//

    @IBAction func reloadPage(_ sender: Any) {
        self.dataArray.removeAll()
        loadData();
    }

    @IBAction func navLeftButtonAction(_ sender: Any) {
        showMenu()
    }

    //==================== Table view options ===================================//

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = dataTableView.dequeueReusableCell(withIdentifier: "cellCon", for: indexPath) as! AttendanceControlCell


        return cell
    }



}

This is Cell file :

class AttendanceControlCell: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)


        initFunc(cell : self)

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!


    }
    func initFunc(cell : UITableViewCell)  {

        let name1 = UILabel()
        name1.numberOfLines = 0
        name1.text = "1 1 11  1 1111 1  1 11  11  11  1 1 1 11 1 1 11 11 1  11 1  11  11  11 "

        let name2 = UILabel()
        name2.numberOfLines = 0
        name2.text = "22 22 2 2 2 2 2 2 2 2 2 2 2  22  22  22 22 2 2  22 2 2 2 2  22 2  2 22 "

        let name3 = UILabel()
        name3.numberOfLines = 0
        name3.text = "33 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3  33 3 3 3  33 33 3 3 3 3 3 333 3 3 3 3 "

        let name4 = UILabel()
        name4.numberOfLines = 0
        name4.text = "444 44 4 44 444 4 4 4 4 4 4 4 4 4 4  44 4 4  44 4 4 4  4 4 4 4 44  44  4 "

        cell.contentView.sv(
            name1,
            name2,
            name3,
            name4
        )
        layout(
            20,
            |name1|,
            20,
            |name2|,
            20,
            |name3|,
            20,
            |name4|
        )

    }

    override func awakeFromNib() {
        super.awakeFromNib()


    }

}

And this is my storyboard: contain only tableview with cell inisde it only

my storyboard image

Please really i need help

Finally, I found the issue.

problem was that I forget to write bottom constraints of bottom last view while I creating views

So this part

layout(
        20,
        |name1|,
        20,
        |name2|,
        20,
        |name3|,
        20,
        |name4|
    )

should be replaced with this

layout(
        20,
        |name1|,
        20,
        |name2|,
        20,
        |name3|,
        20,
        |name4|,
        0
    )

Thanks to all who tried to help me... I hope my answer to help anyone who have sam issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM