2016-07-26 64 views
1

我使用下面的代碼來填充我的函數的標題:斯威夫特 - 更改標題爲標題部分垂直對齊的TableView中

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    switch section { 

    case 0: "SECTION 0" 

    case 1: "SECTION 1" 

    default: break 

    } 

    return nil 
} 

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) 
{ 
    var title = UILabel() 
    title.font = UIFont(name: "HelveticaNeue-Light", size: 12)! 
    title.textColor = UIColor.blackColor() 

    let header = view as! UITableViewHeaderFooterView 
    header.textLabel?.font=title.font 
    header.textLabel?.textColor=title.textColor 
    header.backgroundView?.backgroundColor = UIColor.whiteColor() 

} 

現在,我希望能夠改變垂直下面的圖片標題的部分,像排列:

Changing the vertical alignment of the title in the section of the TableView

我怎麼能這樣做?

回答

2

不幸的是,沒有辦法垂直對齊UILabel中的文本。 This SO貼子更詳細。

但是,您可以通過向父級UIView添加標籤並將其限制在底部來實現類似的效果。那麼你可以在viewForHeaderInSection tableview中返回那個視圖數據源方法

let headerView = UILabel(frame: CGRect(origin: CGPointZero, size: CGSize(width: self.view.frame.width, height: 50))) 
let label = UILabel(frame: CGRect(x: 0, y: 20, width: self.view.frame.width, height: 30)) 
label.text = "Some Text" 
headerView.addSubview(label) 
return headerView 
+0

完美!我刪除了我正在使用的兩個函數,現在我正在使用函數「func tableView(tableView:UITableView,viewForHeaderInSection部分:Int) - > UIView?」中的代碼,如您所說。謝謝! –

+0

太好了。樂意效勞! – Lahav