自我调整TableView单元格的图像和标签

时间:2016-08-12 15:28:25

标签: ios objective-c swift uitableview

你好我有一个动态的TableViewController,其中有一个单元格有一个图像和标签。我已经设定了所有这些的约束。问题是它无法正确查看。

enter image description here

这就是它的观看方式

enter image description here

代码:

 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

    }

我不知道还能做什么。请帮助我如何拥有像我设计的行

3 个答案:

答案 0 :(得分:0)

这里的问题是你的细胞高度 在您的代码中添加以下方法:(根据您选择的值或UITableViewAutomaticDimension替换当然<#cell_height#>)

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

答案 1 :(得分:0)

为了使tableView自行调整大小起作用,您需要在UILabel的顶部和底部添加约束。

enter image description here

答案 2 :(得分:-1)

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 103

我认为你的尝试是正确的,但你需要增加estimatedRowHeight。即300或600.

您需要适当地在Storyboard上添加约束。 所以只需加起来,左,右,底距离约束。 永远不要给宽度和宽度那里的高度限制。

除了你需要在cellForRowAtIndexPath方法中调用cell.contentView.layoutIfNeeded。

我认为它会起作用。

此致 哈里。