2016-11-22 88 views
0

我有我的工作UITableView自尺寸的細胞(UITableViewAutomaticDimension)與新聞內容。而這個工作,它應該:)UITableView是否可以包含一些自我大小的單元格和自定義高度單元格?

現在我走到哪裏我都在新聞單元的中間添加一個小區的一部分,它的高度應該是大小的設備屏幕相同。

我試圖從UITableView類中添加這個高度方法:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    let item = feed[indexPath.row] 
    if item is News { 
     return UITableViewAutomaticDimension 
    } else if item is ConfigurationBit { 
     return view.bounds.size.height 
    } 
    return 0.0 
} 

但這不給正確的結果。你有什麼建議嗎?

UPDATE 以下代碼是正確的。這個問題在其他地方,與這種情況無關。

+0

newsHandler.feed是這個數組同時持有新聞和ConfigurationBit對象 – Vinodh

+0

肯定。我編輯它,所以現在它不那麼困惑。 –

+0

你的代碼沒問題,讓我們打印一下view.bounds.size.height,看看它的大小是否正確。 – Nick

回答

0

我認爲你缺少設置rowHeightestimatedRowHeight的實現代碼如下,請您及時發現代碼中,我使用

var arrayOfCellData = [Any]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     arrayOfCellData = [cellData(cell : 1, text : "asdadas", image : #imageLiteral(resourceName: "mark")), 
          cellconfigData(cell : 2, text : "dasdadad", image : #imageLiteral(resourceName: "mark")), 
          cellData(cell : 3, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark")), 
          cellconfigData(cell : 4, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark")), 
          cellData(cell : 5, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark")), 
          cellconfigData(cell : 6, text : "asraerqsadas", image : #imageLiteral(resourceName: "mark"))] 
     tableView.rowHeight = UITableViewAutomaticDimension 
     tableView.estimatedRowHeight = 245 
    } 


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return arrayOfCellData.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let item = arrayOfCellData[indexPath.row] 
     if item is cellData{ 
      let cell = Bundle.main.loadNibNamed("OwnTableViewCell", owner: self, options: nil)?.first as! OwnTableViewCell 

      cell.mainImageView.image = (arrayOfCellData[indexPath.row] as! cellData).image 
      cell.mainLabel.text = (arrayOfCellData[indexPath.row] as! cellData).text 
      return cell 
     } 
     else 
     { 
      let cell = Bundle.main.loadNibNamed("NewTableViewCell", owner: self, options: nil)?.first as! NewTableViewCell 

      cell.mainImageView.image = (arrayOfCellData[indexPath.row] as! cellconfigData).image 
      cell.mainLabel.text = (arrayOfCellData[indexPath.row] as! cellconfigData).text 

      return cell 
     } 
    } 

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     let item = arrayOfCellData[indexPath.row] 

     if item is cellData{ 
      return UITableViewAutomaticDimension 
     } 
     else if item is cellconfigData{ 
      return 100 
     } 
     return 0.0 
    } 
0
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    let item = feed[indexPath.row] 
    if item is News { 
     let cell = self.tableView(tableObject, cellForRowAt indexPath: indexPath) 
     return cell.frame.size.height 
    } else if item is ConfigurationBit { 
     return view.bounds.size.height 
    } 
    return 0.0 
}