2016-11-21 62 views
0

我對工作如下: Screenshot點擊時。調整的tableView全屏

基本上它是一個UIImageView的另一個的UIImageView以內的頂部(在屏幕的60%),而這需要的40%的UITableView屏幕

和代碼(見下文):

我想實現是調整時,我customCell被點擊的UITableView的全屏的細胞和unsize它是未點擊時的原始狀態。

有什麼辦法可以使這項工作? 在此先感謝。

//Label outlets 

@IBOutlet weak var tableView: UITableView! 


var selectedIndexPath : IndexPath? = nil 


override func viewDidLoad() { 
    super.viewDidLoad() 

//MARK Start of CustomViewCell Code 

    tableView.register(UINib(nibName: "CustomViewCell", bundle: nil), forCellReuseIdentifier: "Cell") 

} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 5 
} 

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! CustomViewCell 

    cell.clipsToBounds = true 

    return cell 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

    let index = indexPath 

    if selectedIndexPath != nil { 
     if index == selectedIndexPath { 
      return 667 
     } else { 
      return 62 
     } 

    } else { 

     return 62} 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    switch selectedIndexPath { 
    case nil: 
     selectedIndexPath = indexPath 
    default: 
     if selectedIndexPath! == indexPath { 
      selectedIndexPath = nil 
     } else { 
      selectedIndexPath = indexPath 
     } 
    } 

    tableView.reloadRows(at: [indexPath], with: .automatic) 

} 

    //MARK End of CustomViewCell Code 
+0

什麼是你'tableViewCell'的,你會全屏顯示的內容?它有一個'imageView'? – Santosh

+0

沒有@Santosh'標籤'和'textView' – j0h4nf

回答

1

除了重新加載具有新高度的單元格之外,還需要更改表格視圖的框架。如果你想使表佔據了整個屏幕,請確保您爲它創建一個IBOutlet,說myTableView,那麼,後:

tableView.reloadRows(at: [indexPath], with: .automatic) 

添加以下代碼:

myTableView.frame = view.bounds 

您還可以創建一個Bool變量來跟蹤表視圖的正常和全屏模式,以及一些CGRect變量來保持初始幀。當你想讓表格視圖返回到其初始框架時,你可能需要這樣的東西:

myTableView.frame = initialTableViewFrame 

希望你明白了。

+0

你已經失去了我@ppalancia 我得到調整'tableView'的想法,我已經宣佈爲一個IBOutlet,但那麼我怎麼讓這個'tableView'成爲一個布爾變量。 我已經嘗試了一些東西,但不能讓它工作。如果你知道一個教程,讓我度過那個很棒的教程。請不要告訴我完整的解決方案,因爲我想盡我所能瞭解自己。 在此先感謝。 – j0h4nf

+0

@ j0h4nf ...我的意思是你的視圖控制器中的表格視圖屬於Bool變量/屬性。看看我是否有時間爲你寫一篇教程。 – ppalancica

0

這是一個純粹的自動佈局方法:

1.In你的界面生成器,創建具有約束[追蹤,領導,頂部,底部,高度]一個UIImageView,使高度> =和優先級爲999 。稍後將在代碼中使用此高度來展開/摺疊邏輯。 Elements Layouts

2.創建一個虛擬的UIView,其高度將在稍後進行編程設置,這將UIView的「推」 /「拉伸」你的身高在創建擴大影響到您的ImageView。將它設置爲隱藏可見性,因爲您不需要將其顯示給用戶。 Elemets Layouts

3.In您的自定義單元格,創建兩個NSLayoutConstraint實例作爲@IBoutlets,一個是身高的UIImageView和一個用於虛擬UIView的高度,然後在你的接口文件之後將它們連接起來。

let lowPriorityHeight : Float = 250; 
let highPriorityHeight : Float = 999; 
class CustomCell: UITableViewCell { 
    @IBOutlet weak var imgVwHeight: NSLayoutConstraint! 
    @IBOutlet weak var tempHeight : NSLayoutConstraint! 
    var isExpanded = false{ 
     didSet{ 
      imgVwHeight.priority = (isExpanded) ? lowPriorityHeight : highPriorityHeight; 
      if isExpanded == true{ 
       tempHeight.constant = ExpandedHeight 
      }else{ 
       tempHeight.constant = NormalHeight 
      } 
     } 
    } 

4.In主類,設置isExpanded爲真或假,以創建效果。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     let cell = tableView.cellForRow(at: indexPath) as! CustomCell 
     cell.isExpanded = !cell.isExpanded 
     tableView.reloadData() 
    } 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell : CustomCell = tableView.dequeueReusableCell(withIdentifier: "kCustomCell", for: indexPath) as! CustomCell 
     return cell 
    } 

您可以下載我的示例實現HERE

+0

感謝@janusfidel,但這不是我正在尋找的解決方案。我真的很想找到一種方法讓我的工作方式開始。 – j0h4nf