【问题标题】:Self sizing TableView cell with image and label带有图像和标签的自调整 TableView 单元格
【发布时间】:2016-08-12 15:28:24
【问题描述】:

您好,我有一个动态 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

    }

我不知道还能做什么。请帮助我怎样才能像我设计的那样排

【问题讨论】:

  • 覆盖tableView:heightForRowAtIndexPath:并返回UITableViewAutomaticDimension

标签: ios objective-c swift uitableview


【解决方案1】:

这里的问题是你的单元格的高度 在您的代码中添加以下方法:(当然用您选择的值或 UITableViewAutomaticDimension 替换 )

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

【讨论】:

    【解决方案2】:

    为了让tableView self sizing 起作用,你需要在UILabel 的顶部和底部添加约束。

    【讨论】:

      【解决方案3】:
      tableView.rowHeight = UITableViewAutomaticDimension
      tableView.estimatedRowHeight = 103
      

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

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

      除此之外,您还需要在 cellForRowAtIndexPath 方法中调用 cell.contentView.layoutIfNeeded。

      我认为它会起作用。

      真诚地, 哈里。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多