2017-02-07 12 views
4

UITableView内部にUITableView内部でUITableViewCellの動的高さを実装したいと思います。どのように私はこの提案を実装することができますか? 私はこのような何かをレイアウトしたい...iOS:動的セル高さを持つUITableViewCell内のUITableView

コード作業:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    let identifier = "OrderHistoryTVCell" let cell: OrderHistoryTVCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? OrderHistoryTVCell 
    let tableInnerContentSize = cell.tableInner.contentSize.height 
    let nn = cell.viewAfterTable.frame.height+tableInnerContentSize 
    return nn 
} 

enter image description here

+0

私たちにあなたの 'prototype-cell'画像を見せてください。 –

+0

ここで必要なビューを更新しました... – PPreeti

+0

拡張可能なtableViewを実装しようとしていますか? – iDeveloper

答えて

0

使用以下:

@IBOutlet weak var tableView: UITableView! //connect the table view 

//do following in viewDidLoad method 
tableView.estimatedRowHeight = 100 //Your estimated height 
tableView.rowHeight = UITableViewAutomaticDimension 

はまた、属性からUILableのこれら2つのプロパティを設定しますストーリーボードの検査官セクション:

言葉に0
  • 改行へ
    • 行が折り返さ

    か、また、コードからこれらのプロパティを設定することができます

    self.lblxyz.numberOfLines = 0 
    self.lblxyz.lineBreakMode = .byWordWrapping 
    

    注 - テーブルビューのセルに適切に制約を追加します。

  • +0

    番号。細胞が異なるサイズであっても役立ちます。私はそれを実装し、それはtextViewのサイズに応じて動作します。データが少ない場合は縮小され、データが多い場合は高さが増加します –

    +0

    これはプロパティですので、VCがロードされたときに呼び出されるため、生成されるたびに呼び出されるデリゲートメソッドが呼び出されます。新しい高さでセルをリロードするとどうなりますか?その時点でこのプロパティは呼び出されていませんが、既に設定されています。うん!あまりにもうまく動作しますが、ユーザーの獲得パフォーマンスに違いがあります。 –

    +0

    apiからデータを取得しているので、tableview reloadも使用しています。それは私のために働くが。最初にtableView.reloadData()行の後に置いていましたが、viewDidLoadで同じものを使用した場合、それも機能します。ここでは、UITableViewAutomaticDimensionは、そのtableViewCellのコンテンツビューで設定された制約に従って常に自動高さを受け入れることを意味します。 –

    -1

    使用のviewDidLoadでこのコード()

    self.tableView.estimatedRowHeight = 350 
    self.tableView.rowHeight = UITableViewAutomaticDimension 
    

    使用もUILableのこれら2つのプロパティを設定し、この委任

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
          return UITableViewAutomaticDimension 
    
         } 
    
    +0

    私のために働いていない... :-( – PPreeti

    +0

    @PPreetiカスタムセルを使用していますか? –

    -2
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
        { 
         self.tableView.estimatedRowHeight = 160 
         return UITableViewAutomaticDimension 
        } 
    

    @IBOutlet weak var label: UILabel! 
    
    1. label.numberOfLines =あなたはこのような何かしなければならない0
    0

    :UI Master-Item #

    1. は、あなたのセクションヘッダもあります。だから、UILabelをとり、それをセクションヘッダーとして追加します。

    2. サブアイテムは、1つのラベルを含むprtotype-cellセルです。

    セル内のラベルの制約。あなたのcell

    • 次のように最初の制約にUIViewを追加します。
      1. トップ、ボトム、0としてsuperViewにつながる、ボトム。
    • ラベルを追加し、次のように制約を与えます。
      1. 上側、下側、下側、UIViewとして8
      2. 要件に応じて高さの制約を与えます。
      3. =から>=までの高さの関係を指定します。
      4. storyboardからlabelプロパティ行を0に設定します。

    delegateとのtableViewのdataSourceをバインドすることを忘れないでください

    //MARK:- TableView 
    
    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{ 
        return 60 // return header height 
    } 
    
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    
        // return your section header i.e. master item label 
    
    } 
    
    public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat{ 
        return 50; 
    } 
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ 
        return UITableViewAutomaticDimension 
    } 
    
    public func numberOfSections(in tableView: UITableView) -> Int{ 
    // return number of section i.e. total count of master item. 
    } 
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 
    // returns number of cells in each section 
    } 
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
        // return your custom cell here 
        //eg: 
        // cell.labelFood.text = your data 
    } 
    

    NOTEにこのラインを実装します。

    +0

    私は自動レイアウトを使用していません。デザインパターンはスケーリングされています – PPreeti

    +0

    @PPreetiまず第一に、 'heightForRow 'メソッドであり、' cellForRow'メソッドでのみ行います。2番目のコードを追加して、実際に何をしているのか見てみましょう。 –

    関連する問題