2016-08-12 180 views
0

你好我有一個動態的TableViewController,其中有一個單元格有一個圖像和標籤。我已經設定了所有這些限制。問題是它沒有正確觀察。自定義帶有圖像和標籤的TableView單元

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 

    } 

不知道還能做什麼。請幫助我怎麼能像我設計的排

+0

覆蓋'的tableView:heightForRowAtIndexPath:'和'返回UITableViewAutomaticDimension' – Brandon

回答

0

這裏的問題是你的細胞的高度 在你的代碼中添加以下方法:(當然<#cell_height#>替換您選擇的值,或通過UITableViewAutomaticDimension)

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

三江源非常多......解決了它..謝謝 – hellosheikh

-1
tableView.rowHeight = UITableViewAutomaticDimension 
tableView.estimatedRowHeight = 103 

我覺得你艱難的是對的,但你需要增加estimatedRowHeight。即300或600.

而且你需要在Storyboard上適當添加約束。所以只需加起來,左,右,底部的距離限制。 千萬不要在此處給出寬度&高度限制。

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

我認爲它會工作。

此致Harrel。 Harri。

0

爲了使tableView自行調整大小,您需要將約束添加到UILabel的頂部和底部。

enter image description here