在 UITableView 的部分标题文本下方添加一些边距 [英] adding some margin below section header text of a UITableView

查看:23
本文介绍了在 UITableView 的部分标题文本下方添加一些边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了标题文本的样式:

I have styled the header text:

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 添加了一些边距/填充顶部.有什么方法可以添加一些顶部/底部边距/填充?

And I would like to add some margin / padding below it before the section of the table cells start. I have added some margin / padding top with heightForHeaderInSection. Any way to add some top / bottom margin / padding?

推荐答案

titleForHeaderInSection 方法将具有默认的节标题框架.您可以使用 viewForHeaderInSection 方法对其进行自定义.试试这个代码:

titleForHeaderInSection method will have the default section header frame. You can use viewForHeaderInSection method to customize it. Try this code :

  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
}

希望这对你有用.

这篇关于在 UITableView 的部分标题文本下方添加一些边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆