最新消息:20210917 已从crifan.com换到crifan.org

[已解决]swift中设置UITableView的section的字体和颜色

iOS crifan 4115浏览 0评论

之前就知道

可以通过直接实现:

viewForHeaderInSection

去设置对应的字体和颜色

然后去搜:

swift uitableview section font

发现还有其他办法:

swift ios 8 change font title of section in a tableview – Stack Overflow

swift – How do I set Section attributes in UITableView? – Stack Overflow

然后去用:

    override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

        let header = view as! UITableViewHeaderFooterView

        header.textLabel?.font = UIFont(name: "Futura", size: 38)!

        header.textLabel?.textColor = UIColor.lightGrayColor()

    }

结果出错:

CustomerDetailViewController.swift:314:19: Method does not override any method from its superclass

如图:

-》猜测是:

是变成代理了?

所以去掉overvide试试:

    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

        let header = view as! UITableViewHeaderFooterView

        header.textLabel?.font = UIFont(name: "Futura", size: 38)!

        header.textLabel?.textColor = UIColor.lightGrayColor()

    }

果然可以了。

再去看看定义,果然是有的:

public protocol UITableViewDelegate : NSObjectProtocol, UIScrollViewDelegate {

    // Display customization

    @available(iOS 2.0, *)

    optional public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)

    @available(iOS 6.0, *)

    optional public func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)

最后自己用:

let CustomerDetailSectionHeaderColor:UIColor = UIColor(hexString: "#999999")!

let CustomerDetailSectionHeaderFont:UIFont = UIFont.systemFontOfSize(12)

    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

        guard let sectionHeaderView = view as? UITableViewHeaderFooterView else {

            return

        }

        sectionHeaderView.textLabel?.font = CustomerDetailSectionHeaderFont

        sectionHeaderView.textLabel?.textColor = CustomerDetailSectionHeaderColor

    }

实现了对应的效果:

转载请注明:在路上 » [已解决]swift中设置UITableView的section的字体和颜色

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
93 queries in 0.184 seconds, using 23.27MB memory