簡體   English   中英

具有圖像和標簽的自定義TableView單元格的大小

[英]Self sizing TableView cell with image and label

您好,我有一個動態的TableViewController,其中有一個單元格包含一個圖像和標簽。 我對所有這些都設置了約束。 問題是無法正確查看。

在此處輸入圖片說明

這是它的觀看方式

在此處輸入圖片說明

碼:

 override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 103

            }



    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        tableView.setNeedsLayout()
        tableView.layoutIfNeeded()
        self.tableView.reloadData()





    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        tableView.reloadData()
    }


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1


    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return newarray.count
           }

    override
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("requestcell", forIndexPath: indexPath) as! NewRequestTableViewCell

       let image : UIImage = UIImage(named: "placeholder_product_image")!
        cell.imageView!.image = image
        cell.requestTitle.text = "IPHONE 6s PLUS"
        cell.DestinationCountry.text = "Pakistan"

        return cell

    }

我不知道該怎么辦 請幫助我如何像我設計的那樣排

這里的問題是單元格的高度,請在代碼中添加以下方法:(用您選擇的值或UITableViewAutomaticDimension替換課程<#cell_height#>)

override func tableView(_ tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return <#cell_height#>;
}

為了使tableView自動調整大小,您需要在UILabel的頂部和底部都添加約束。

在此處輸入圖片說明

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 103

我認為您的嘗試是正確的,但是您需要增加估計的RowHeight。 即300或600。

並且您需要在Storyboard上適當添加約束。 因此,只需累加,左,右,下距離限制。 永遠不要在此處給出寬度和高度限制。

另外,您需要僅在cellForRowAtIndexPath方法中調用cell.contentView.layoutIfNeeded。

我認為那會起作用。

真誠的,哈里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM