Swift-如何为UITableView制作自定义标题? [英] Swift - how to make custom header for UITableView?

查看:119
本文介绍了Swift-如何为UITableView制作自定义标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在表格中添加自定义标题

I need to add custom header to my table

我尝试

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

    let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 18))
    let label = UILabel(frame: CGRect(x: 20, y: 20, width: 50, height: 50))
    label.text = "TEST TEXT"
    label.textColor = UIColor.whiteColor()

    self.view.addSubview(view)

    return view
}

但这不起作用,桌上什么也没看到

but this doesn't work, I see nothing on table

我在做什么错?也许还有其他方法?

What am I doing wrong ? Or maybe there is another ways ?

推荐答案

您是否在viewDidLoad中设置了节标题高度?

Did you set the section header height in the viewDidLoad?

self.tableView.sectionHeaderHeight = 70

另外,您应该替换

self.view.addSubview(view)

作者

view.addSubview(label)

最后,您必须检查一下镜框

Finally you have to check your frames

let view = UIView(frame: CGRect.zeroRect)

,并最终获得所需的文本颜色,因为当前看来是白色.

and eventually the desired text color as it seems to be currently white on white.

这篇关于Swift-如何为UITableView制作自定义标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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