2017-01-01 79 views
0

我將xcode從7.3.1升級到8並轉換爲swift 2.2,除了故事板是一團糟之外,一切工作除了以編程方式添加UIView。addSubview轉換後不工作

之前工作正常,現在沒有任何顯示,也沒有錯誤。有任何想法嗎?

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    //create view 
    let vw = UIView() 
    vw.backgroundColor = UIColor(red: 0.9137, green: 0.9176, blue: 0.9216, alpha: 1.0) /* #e9eaeb */ 

    //create label inside view 
    let label: UILabel = UILabel() 
    label.frame = CGRectMake(0, 0, 200, 28) 
    label.textAlignment = NSTextAlignment.Left 
    label.textColor = UIColor.grayColor() 
    label.font = label.font.fontWithSize(13) 

    if !clearSearch { 
     label.text = " Recent Searches" 
    } else { 
     label.text = " Search Results" 
    } 
    vw.addSubview(label) 

    //create button inside view 
    let button: UIButton = UIButton() 
    button.frame = CGRectMake(self.view.frame.size.width - 45, 0, 40, 28) 
    button.titleLabel?.font = UIFont.systemFontOfSize(13) 
    button.setTitle("Clear", forState: UIControlState.Normal) 
    button.addTarget(self, action: #selector(deleteSearches), forControlEvents: UIControlEvents.TouchUpInside) 
    button.setTitleColor(UIColor(red: 0.3216, green: 0.7176, blue: 0.5333, alpha: 1.0), forState: UIControlState.Normal) 
    vw.addSubview(button) 

    return vw 
} 

感謝您的任何幫助。

+0

這一切都很好。你應該驗證vw在返回語句中是否爲零(它應該是),然後檢查看看調用代碼中發生了什麼。我懷疑這個問題在那裏。您還應該嘗試使用Debug-> View Debugging-> Capture View Hierarchy工具來查看正在發生的事情。 –

+0

嘿,謝謝你的快速回復。是的,它看起來並不是那個功能塊已經被調用了,很奇怪,我沒有改變任何東西。我也嘗試了像你所建議的那樣的調試,它也沒有出現在那裏。 – user3712837

+0

找出來了,現在需要heightForHeaderInSection。 – user3712837

回答

0

看起來這是現在需要的。

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 28 
}