2016-02-12 35 views
2

我已經樣式的標題文字的節頭文字一些保證金:下面添加一個UITableView

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? TableViewCell 

    cell!.titleLabel!.text = sections[indexPath.section].objects[indexPath.row].name 
    cell!.datetimeLabel!.text = "on " + sections[indexPath.section].objects[indexPath.row].date 

    return cell! 
} 

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

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return sections[section].heading // Open Events 
} 

而且我想細胞開始的錶款之前,在它下面添加一些保證金/填充。我已在heightForHeaderInSection上添加了一些邊距/填充頂部。任何方式來添加一些頂部/底部邊距/填充?

enter image description here

+0

您是否可以不使標題高度更大,並將「打開事件」標籤向上移動? – Fonix

+0

使heightForHeaderInSection爲120並嘗試。 – Signare

+0

@Fonix「打開事件」不是標籤,而是我在代碼 – Sayanee

回答

5

titleForHeaderInSection方法將具有返回而不是使用默認頭的視圖默認節頭框。您可以使用viewForHeaderInSection方法對其進行自定義。試試這個代碼:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerFrame = tableView.frame 

    let title = UILabel() 
    title.frame = CGRectMake(10, 10, headerFrame.size.width-20, 20) //width equals to parent view with 10 left and right margin 
    title.font = title.font.fontWithSize(14) 
    title.text = self.tableView(tableView, titleForHeaderInSection: section) //This will take title of section from 'titleForHeaderInSection' method or you can write directly 
    title.textColor = UIColor.blueColor() 

    let headerView:UIView = UIView(frame: CGRectMake(0, 0, headerFrame.size.width, headerFrame.size.height)) 
    headerView.addSubview(title) 

    return headerView 
} 

希望這會對你有用。

+1

謝謝,是的,它現在的作品有點調整https://gist.github.com/sayanee/a164f7f22423c17494e6。我想知道,如果'CGRectMake'的寬度的第三個參數可以是動態的以取得父寬度。 – Sayanee

+1

這裏是一個截圖,如果你想更新https://www.dropbox.com/s/ook1ymq6jglsa0o/Screenshot%202016-02-12%2013.59.13.png?dl=0 – Sayanee

+0

你的意思是,你想標題標籤的高度爲headerView高度相同嗎? – iRiziya

1

使用viewForHeaderInSection:和與它下面的正確間距的標籤在titleForHeaderInSection

看到this answer爲例

相關問題