2016-04-08 11 views
2

最後のセル/行(mapView)が空のスペースを埋めるようにします。 enter image description hereuitableviewの最後の行を空白スペースに埋め込む方法

マイtableviewcontrollerコード

class CustomTableViewController: UITableViewController { 

    let fakeArray = ["Row1","Row2","Row3"] // Array can contains more items 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return fakeArray.count+1 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     if(indexPath.row != fakeArray.count){ 
      let cellIdentifier = "TestTableViewCell" 
      let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TestTableViewCell 
      cell.rowName.text = fakeArray[indexPath.row] 
      return cell 
     }else{ 
      let cellIdentifier = "MapTableViewCell" 
      let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MapTableViewCell 
      return cell 
     } 

    } 

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     if(indexPath.row != fakeArray.count){ 
      return 44 
     }else{ 
      return 80 
     } 
    } 

} 

は、時々私は、マップ前にディスプレイに複数の行を持つことになります。したがって、下の空白のスペースは変わる可能性があります。コースの空きスペースもiPhoneの画面サイズによって変わります。

+0

最後の行の行の高さを増やすことができます。 –

+2

私は、テーブルビューのフッタービューにマップを配置します。 – dasdom

+0

地図の前にたくさんの行を表示して、地図が表示されないことはありますか?それでは? –

答えて

5

空きスペースの量を把握し、マップセルの高さをそれに設定する必要があります。

最初に、テーブルビュー自体の上部と下部をスーパービューに制限し、画面全体を占有するようにします。

あなたのテーブルビューの可視高さは次のようになりますheightForRowAtIndexPathで

self.tableView.bounds.size.height 

は、マップセル上のセルの高さを計算し、それとテーブルビューの可視高さとの差を返します。もちろん

let otherCellsHeight: CGFloat = fakeArray.count * 44 

    return self.tableView.bounds.size.height - otherCellsHeight 

、あなたはまた、マップのセルの計算高さがちょうど安全のために、いくつかの最小の高さよりもマイナスか小さくないことを確認する必要があります

let availableSpace = self.tableView.bounds.size.height - otherCellsHeight 

    return (availableSpace > 80) ? availableSpace : 80 
0

これは別のスレッドで同様の質問に答えました。

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{ 

    if indexPath.row == 0 { 
      return firstRowHeight 
    } 
    else if indexPath.row == 1 { 
      return secondRowHeight 
    } 
    else if indexPath.row == 2 { 
      return thirdRowHeight 
    } 
    else { 
      return tableView.frame.size.height - firstRowHeight - secondRowHeight - thirdRowHeight 
    } 

} 
関連する問題