繁体   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