Objective c UITableView标题标题标题部分显示所有大写

Objective c UITableView标题标题标题部分显示所有大写,objective-c,uitableview,header,capitalization,Objective C,Uitableview,Header,Capitalization,我正在使用TitleForHeaderSection显示UITableView节的标题。它在ios6sdk中运行良好,但ios7sdk在所有CAPS中都显示了头 我想这是苹果最新的人机界面指南的一部分;其中的所有示例都显示了所有CAP中的标题。此外,“我的iPhone”上“设置”中的所有章节标题都是大写的 然而,我想知道是否有办法解决这个问题。通常情况下,如果可以提高一致性,我不介意显示大写字母,但当我需要在节标题中显示人名时,这有点尴尬 有人知道如何使用大写吗?我找到的一个解决方案是利用UIT

我正在使用TitleForHeaderSection显示UITableView节的标题。它在ios6sdk中运行良好,但ios7sdk在所有CAPS中都显示了头

我想这是苹果最新的人机界面指南的一部分;其中的所有示例都显示了所有CAP中的标题。此外,“我的iPhone”上“设置”中的所有章节标题都是大写的

然而,我想知道是否有办法解决这个问题。通常情况下,如果可以提高一致性,我不介意显示大写字母,但当我需要在节标题中显示人名时,这有点尴尬


有人知道如何使用大写吗?

我找到的一个解决方案是利用
UITableViewHeaderFooterView

而不是

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"some title";
}

令人恼火的缺点是,表格视图将不再自动为您调整节标题高度。因此,如果您的收割台高度不同,您必须执行以下操作:

- (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;
}

我真的不喜欢这样硬编码布局信息(插图),因为它可能在未来的版本中被破坏。如果有人有更好的方法来获取/设置收割台高度,我洗耳恭听

是的,我们遇到了与此非常相似的问题,我自己的解决方案如下:

Apple UITableViewHeaderFooterView文档(指向它的链接很长,但你可以通过你喜欢的搜索引擎轻松找到它)说,你可以访问标题视图的文本标签,而不必通过ViewForHeaderSection方法格式化你自己的视图

textLabel视图的主文本标签。(只读)

@属性(非原子、只读、保留)UILabel*textLabel讨论 访问此属性中的值会导致视图创建 用于显示详细信息文本字符串的默认标签。如果你正在管理 通过将子视图添加到contentView,您可以自己查看视图的内容 属性,则不应访问此属性

标签的大小以最佳方式适合内容视图区域 根据字符串的大小,可能会出现错误。它的大小也进行了调整 取决于是否存在详细信息文本标签

通过一些额外的搜索,修改标签文本的最佳位置是willDisplayHeaderView方法(中建议)

因此,我提出的解决方案非常简单,只需在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”子句,因为节号也会传递给该方法,所以希望它能让您有选择地用大写字母显示您的用户/客户端名称。

我发现的解决方案是在“titleForHeaderInstruction”方法中添加标题

然后调用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;
}

对我有效的解决方案是创建一个简单的
UILabel
实例变量,用作节头视图。(您的需求可能不同,但对我来说,我只需要在标题中包含文本…) 然后,在
视图forheaderinsection
中,我根据需要设置标签,并将此标签作为标题视图返回

然后,每当我想更新标题中的文本时,我只需直接更改标签的文本

在.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...";
…或者您可以简单地呼叫:

[self refreshMySectionHeaderLabel];
sectionHeaderLabel.text = @"The new text...";

注意:我只有1个部分(第0部分)。您可能需要解释多个部分…

谢谢@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方法完成了

如果您只想保持字符串的原样,只需执行以下操作:

- (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];
    }
}
Swift解决方案:

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)
    }
}

在Swift中

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let headerView = view as! UITableViewHeaderFooterView
    headerView.textLabel.text = "My_String"
}

使用RubyMotion/RedPotion,将其粘贴到您的桌面屏幕:

  def tableView(_, willDisplayHeaderView: view, forSection: section)
    view.textLabel.text = self.tableView(_, titleForHeaderInSection: section)
  end

我认为现在发生的是,在那里的某个地方,文本被设置为所有大写。这个小把戏将文本重置回原来的状态。对我来说很有魅力

最简单的解决方案如下:Swift 2.x

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

        header.textLabel?.text = header.textLabel?.text?.capitalizedString
}

一个线性解决方案,基于@Dheeraj D答案:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
  (view as? UITableViewHeaderFooterView)?.textLabel?.text = (view as? UITableViewHeaderFooterView)?.textLabel?.text?.capitalized
}
Swift 3.x:

if let headerView = view as? UITableViewHeaderFooterView {
    headerView.textLabel?.text? = headerView.textLabel?.text?.capitalized ?? ""
}

除了@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:

-(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];
}
请记住,此代码仅适用于第1节。如果您想浏览所有部分,您需要为它们添加支持

SWIFT

在实现中,我发现您需要在titleForHeaderInSectionwillDisplayHeaderView函数中指定节头文本,其他vise头隐藏

    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
        }

    }

Swift 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
    }

超级的!我将您的willDisplayHeaderView方法复制到我的代码UITableViewController中,它立即起作用。德克萨斯州!如果你想显示一个完整的句子,因为它会大写每个单词,那么这是行不通的。Raaah,@moon4e,要有创意!例如,
title=[NSString stringWithFormat:@“%@%@,[[title substringToIndex:1]大写字符串],[title substringFromIndex:1]小写字符串]]
所以您需要更复杂的东西,比如
-(UIView*)tableView:(UITableView*)tableView视图ForHeaderSection:(NSInteger)section
并根据字符串创建视图。我遇到了一个问题,来自
视图ForHeaderSection:
的视图最初显示了一个默认/未设置样式的标题,然后很快用样式化版本重新加载。在
中设置视图样式将解决此问题。header对象是什么?它是UITableViewHeaderFooterView对象这还不够,您需要提供真正的c