2016-08-24 72 views
2

我正在將我的表視圖的標題高度設置爲0以將其動畫化。我現在用的是當前的代碼:使用動畫調整UITableView標題的大小問題

var newRect = headerView.frame 
    newRect.size.height = 0 
    UIView.animateWithDuration(0.6) { 
     self.headerView.frame = newRect 
    } 

headerView是一個自定義的UIView和像這樣使用:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 


     return headerView 

    } 

當我運行的代碼,它似乎動畫headerView的高度正好一半大小而不是0.任何想法,我可能在這裏做錯了嗎?任何指針都會很棒。

回答

1

您只需使用heightForHeaderInSectionUITableView重新加載系統。貝婁,有一個如何去做的例子。這是一個虛擬的例子(當使用按下單元格時,它會使節標題(dis)出現。

var displayExpandedHeader = true 
let expandedHeight = CGFloat(50) 
let colapsedHeight = CGFloat(0) 
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    return headerView 
} 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    tableView.beginUpdates() 
    displayExpandedHeader = !displayExpandedHeader 
    tableView.reloadSections(NSIndexSet(index: indexPath.section), withRowAnimation: (!displayExpandedHeader) ? .Top : .Bottom) 
    tableView.endUpdates() 
} 
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return (displayExpandedHeader) ? expandedHeight : colapsedHeight 
} 
+0

這幫了我,謝謝你的回答。 – Kex