我想在我的应用程序中自定义标题部分,但它在静态单元格中。我试图通过包含标识符使一个单元格成为我的标题,并添加一个新文件来控制该单元格,但这并不起作用。我试图将一个对象拖到新文件中,但它无法完成。那么如何自定义节头?我的接近方式是好的吗?
答案 0 :(得分:19)
There are a couple of ways to customize the header section in a UITableView
. For instance, if all you want to do is change the text, you can do so in the attributes inspector while making sure your TableViewSection is selected:
However, if you want the ability to do customizations such as text size, tont, capitalizations - any customizations inherent to UILabels, you'll need to override this method from the UITableViewController
:
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel.textColor = UIColor(red: 243/255, green: 153/255, blue: 193/255, alpha: 1.0)
header.textLabel.font = UIFont(name: "Helvetica Neue", size: 18)
header.textLabel.text = "About Us"
header.textLabel.frame = header.frame
header.textLabel.textAlignment = NSTextAlignment.Left
}
For example, in the code above, I took the header that was passed as a parameter and configured the textColor, font, text, alignment - really anything you can do on the UILabel can be done here.
Before customization
After customization
答案 1 :(得分:0)
您也可以使用Nib自定义标题部分。简单的例子如下。
HeaderView.swift
import UIKit
class HeaderView: UITableViewHeaderFooterView {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var titleBackgroundView: UIView!
static var nib: UINib {
return UINib(nibName: identifier, bundle: nil)
}
static var identifier: String {
return String(describing: self)
}
override func awakeFromNib() {
titleBackgroundView.layer.shadowColor = UIColor.gray.cgColor
titleBackgroundView.layer.shadowOpacity = 0.5
titleBackgroundView.layer.shadowOffset = CGSize(width: 0, height: 5)
titleBackgroundView.layer.shadowRadius = 5
}
}
ProfileTableViewController.swift
import UIKit
class ProfileTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(HeaderView.nib, forHeaderFooterViewReuseIdentifier: HeaderView.identifier)
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: HeaderView.identifier) as! HeaderView
switch section {
case 0:
headerView.titleLabel.text = "Profile"
return headerView
case 1:
headerView.titleLabel.text = "Social"
return headerView
default:
return UIView()
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
}
答案 2 :(得分:0)
这是一种简单的方法,可以在不设置新字符串的情况下执行此操作,只需将文本放在UITableview中使用的标题中,并将静态单元格添加到其中,并在代码中为标题添加此覆盖
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
((UITableViewHeaderFooterView*)view).textLabel.text = [((UITableViewHeaderFooterView*)view).textLabel.text capitalizedString];
}
}