我正在使用 titleForHeaderInSection 来显示 UITableView 部分的标题。它适用于 iOS6 SDK,但 iOS7 SDK 以所有大写字母显示标题。
我猜这是 Apple 更新的人机界面指南的一部分;那里的所有示例都显示全部大写的标题。此外,我 iPhone 上“设置”中的所有部分标题都全部大写。
但是,我想知道是否有办法解决这个问题。通常,如果这样可以提高一致性,我不介意显示大写字母,但是当我需要在部分标题中显示人名时,这有点尴尬。
有人知道如何解决大写问题吗?
我正在使用 titleForHeaderInSection 来显示 UITableView 部分的标题。它适用于 iOS6 SDK,但 iOS7 SDK 以所有大写字母显示标题。
我猜这是 Apple 更新的人机界面指南的一部分;那里的所有示例都显示全部大写的标题。此外,我 iPhone 上“设置”中的所有部分标题都全部大写。
但是,我想知道是否有办法解决这个问题。通常,如果这样可以提高一致性,我不介意显示大写字母,但是当我需要在部分标题中显示人名时,这有点尴尬。
有人知道如何解决大写问题吗?
是的,我们有一个非常相似的问题,我自己的解决方案如下:
Apple UITableViewHeaderFooterView 文档(指向它的链接非常长,但您可以使用您最喜欢的搜索引擎轻松找到它)说您可以访问标题视图的 textLabel,而无需通过 viewForHeaderInSection 方法格式化您自己的视图。
textLabel 视图的主要文本标签。(只读)
@property(nonatomic, readonly, retain) UILabel *textLabel 讨论访问此属性中的值会导致视图创建默认标签以显示详细文本字符串。如果您通过将子视图添加到 contentView 属性来自己管理视图的内容,则不应访问此属性。
标签的大小会根据字符串的大小以最佳方式适合内容视图区域。它的大小也会根据是否存在详细文本标签进行调整。
通过一些额外的搜索,修改标签文本的最佳位置是 willDisplayHeaderView 方法(在How to implement `viewForHeaderInSection` on iOS7 style?中建议)。
所以,我想出的解决方案非常简单,只需在 titleForHeaderInSection 实际设置文本标签字符串后进行转换:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//I have a static list of section titles in SECTION_ARRAY for reference.
//Obviously your own section title code handles things differently to me.
return [SECTION_ARRAY objectAtIndex:section];
}
然后只需调用 willDisplayHeaderView 方法来修改它的外观:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
tableViewHeaderFooterView.textLabel.text = [tableViewHeaderFooterView.textLabel.text capitalizedString];
}
}
您可以在其中放入自己的“if”或“switch”子句,因为节号也传递给该方法,因此希望它允许您有选择地以大写单词显示您的用户/客户端名称。
我找到的解决方案是在“titleForHeaderInSection”方法中添加标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"Table Title";
}
然后调用 willDisplayHeaderView 方法更新:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.textLabel.textColor = [UIColor darkGrayColor];
header.textLabel.font = [UIFont boldSystemFontOfSize:18];
CGRect headerFrame = header.frame;
header.textLabel.frame = headerFrame;
header.textLabel.text= @"Table Title";
header.textLabel.textAlignment = NSTextAlignmentLeft;
}
如果您只想保持字符串原样,您可以简单地执行以下操作:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isKindOfClass:[UITableViewHeaderFooterView class]] && [self respondsToSelector:@selector(tableView:titleForHeaderInSection:)]) {
UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
headerView.textLabel.text = [self tableView:tableView titleForHeaderInSection:section];
}
}
快速解决方案:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView, self.respondsToSelector(Selector("tableView:titleForHeaderInSection:")) {
headerView.textLabel?.text = self.tableView(tableView, titleForHeaderInSection: section)
}
}
在斯威夫特,
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let headerView = view as! UITableViewHeaderFooterView
headerView.textLabel.text = "My_String"
}
我发现的一种解决方案是利用UITableViewHeaderFooterView.
代替
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"some title";
}
做
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *identifier = @"defaultHeader";
UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
if (!headerView) {
headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:identifier];
}
headerView.textLabel.text = @"some title";
return headerView;
}
恼人的缺点是表格视图将不再为您自动调整节标题高度。因此,如果您的标题高度不同,您必须执行以下操作:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
id headerAppearance = [UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil];
UIFont *font = [headerAppearance font];
CGFloat viewWidth = CGRectGetWidth(tableView.frame);
CGFloat xInset = 10;
CGFloat yInset = 5;
CGFloat labelWidth = viewWidth - xInset * 2;
CGSize size = [sectionInfo.name sizeWithFont:font constrainedToSize:CGSizeMake(labelWidth, MAXFLOAT)];
return size.height + yInset * 2;
}
我真的不喜欢以这种方式硬编码布局信息(插图),因为它可能会在未来的版本中中断。如果有人有更好的解决方案来获取/设置标题高度,我会全神贯注。
谢谢@Animal451。这是更通用的解决方案,适用于任何类型的标头字符串。
// We need to return the actual text so the header height gets correctly calculated
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.headerString;
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setText:self.headerString]; //String in the header becomes uppercase. Workaround to avoid that.
}
为避免重复,可以在其他任何地方声明标题字符串。我已经在 -viewDidLoad 方法中完成了。
除了@Animal451 的帖子。对于 swift 3,您可以使用
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard section == 0 ,
let tableViewHeaderFooterView = view as? UITableViewHeaderFooterView
else { return }
tableViewHeaderFooterView.textLabel?.text = "Your awesome string"
}
然后忽略 - titleForHeaderInSection:
请记住,此代码仅适用于第一部分。如果您想浏览所有部分,则需要添加对它们的支持
最简单的解决方案如下:Swift 2.x
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
header.textLabel?.text = header.textLabel?.text?.capitalizedString
}
斯威夫特 5
静态 TableViewController 和故事板部分标题。下面的代码将使用大写的第一个字母。
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let titleView = view as! UITableViewHeaderFooterView
titleView.textLabel?.text = titleView.textLabel?.text?.capitalized
}
斯威夫特 3.x:
if let headerView = view as? UITableViewHeaderFooterView {
headerView.textLabel?.text? = headerView.textLabel?.text?.capitalized ?? ""
}
对我有用的解决方案是创建一个简单的UILabel 实例变量以用作部分标题视图。(您的需求可能不同,但对我来说,我只需要在标题中包含文本...)然后,在内部viewForHeaderInSection,我根据需要设置标签,并将此标签作为标题视图返回。
然后,每当我想更新标题中的文本时,我只需直接更改标签的文本。
在 .h 文件中:
@property (nonatomic, retain) UILabel *sectionHeaderLabel;
然后在 .m 文件中:
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [self refreshMySectionHeaderLabel]; // I created this handy little method to update the header text
}
// Get the latest text for the section header...
-(UILabel *) refreshMySectionHeaderLabel {
// Initialise the first time.
if( sectionHeaderLabel == nil ) {
sectionHeaderLabel = [[UILabel alloc] initWithFrame: CGRectNull];
}
// ...
// May also set other attributes here (e.g. colour, font, etc...)
// Figure out the text...
sectionHeaderLabel.text = @"Your updated text here...";
return sectionHeaderLabel;
}
当我想更新我的部分标题时,我只需调用:
[self refreshMySectionHeaderLabel];
...或者您可以简单地调用:
sectionHeaderLabel.text = @"The new text...";
请注意:我只有 1 个部分(第 0 部分)。您可能需要考虑多个部分...
迅速
在实现中,我发现您需要在titleForHeaderInSection和willDisplayHeaderView函数中指定节标题文本,否则标题隐藏。
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let sectionHeader = datasource[section].sectionHeader
// Header Title
return sectionHeader
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let header = view as? UITableViewHeaderFooterView else { return }
header.textLabel?.textColor = UIColor.darkGray
header.textLabel?.font = UIFont.systemFont(ofSize: 20, weight: .semibold)
header.textLabel?.frame = header.frame
// Header Title
header.textLabel?.text = datasource[section].sectionHeader
}
}
一种基于@Dheeraj D 答案的班轮解决方案:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
(view as? UITableViewHeaderFooterView)?.textLabel?.text = (view as? UITableViewHeaderFooterView)?.textLabel?.text?.capitalized
}
使用 RubyMotion / RedPotion,将其粘贴到您的 TableScreen 中:
def tableView(_, willDisplayHeaderView: view, forSection: section)
view.textLabel.text = self.tableView(_, titleForHeaderInSection: section)
end
我认为正在发生的事情是那里的某处文本被设置为全部大写。这个小技巧将文本重置为原来的样子。对我来说就像一个魅力!
使用方法viewForHeaderInSection创建标题标题。
在viewDidLoad注册一个 UITableViewHeaderFooterView
tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "HeaderFooterViewID")
添加委托
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderFooterViewID")
var config = header?.defaultContentConfiguration()
let title = "My Header Title"
let attribute = [ NSAttributedString.Key.foregroundColor: UIColor.blue ]
config?.attributedText = NSAttributedString(string: title, attributes: attribute)
header?.contentConfiguration = config
return header
}
您可以更多地调整属性文本,参考