【问题标题】:UITableView section header on right instead of default on left右侧的 UITableView 部分标题而不是左侧的默认标题
【发布时间】:2012-10-01 11:16:40
【问题描述】:

我正在开发一个应用程序,它要求部分的标题应该在右侧而不是默认的左侧。

我搜索了很多极客建议实现:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static UIView *headerView;

    if (headerView != nil)
        return headerView;

    NSString *headerText = NSLocalizedString(@"البحث الأخيرة", nil);

    // set the container width to a known value so that we can center a label in it
    // it will get resized by the tableview since we set autoresizeflags
    float headerWidth = 150.0f;
    float padding = 10.0f; // an arbitrary amount to center the label in the container

    headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)];
    headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    // create the label centered in the container, then set the appropriate autoresize mask
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(padding, 0, headerWidth - 2.0f * padding, 44.0f)];
    headerLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    headerLabel.textAlignment = UITextAlignmentRight;
    headerLabel.text = headerText;

    [headerView addSubview:headerLabel];

    return headerView;
}

但是,当我尝试增加标题视图框架的 x 坐标时,它不会影响视图的位置。而且它始终位于表格视图的中心。

我需要标题在右边。

【问题讨论】:

  • 不要使用flexibleWidth,你不能强制它为全宽吗?然后视图将占用整个宽度并右对齐文本。
  • 您已经选择了一个基本正确的答案,但是添加空格来获得空白是一个真正的技巧。解决问题的正确方法是创建一个空视图(或具有背景颜色的视图)并将其用作 headerView。然后将 UILabel 添加到该容器视图,并将其从右侧偏移一点。这个概念——使用一个空视图作为容器来放置其他视图是一种常见且有用的工具。
  • @DavidH 你是对的。添加空格的想法并不好。虽然它暂时对我有用。正如我在我的问题中提到的那样,视图总是在我需要它位于右侧的中心位置。设置框架不起作用。标签也是右对齐的。我知道使用 UIView 有更多的自定义选项要好得多。感谢您的评论!
  • 还有一件事。按照 ilight 的建议返回 UILabel 会得到与默认值完全相同的外观。

标签: iphone objective-c ios uitableview


【解决方案1】:

这对我有用,但是可以根据您的需要使用框架的坐标来对齐

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] init];
    label.text=@"header title";
    label.backgroundColor=[UIColor clearColor];
    label.textAlignment = NSTextAlignmentRight;
    return label;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

【讨论】:

  • 谢谢!有效 :)。但是,文本的开头几乎触及视图边框。我在文本中添加了一些空格,现在文本向左移动了一点。
  • UITextAlignmentRight 已弃用,应使用 NSTextAlignmentRight
【解决方案2】:

我在使用上述解决方案更改文本时遇到了麻烦,这对我有用,并且位置与表格视图的后缘有适当的间距。 Swift 中的 PS 解决方案

UITableViewDataSource

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 30
}

UITableViewDelegate

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Header Title"
}

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

    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.textLabel?.font = UIFont(name: "AvenirNext-Regular", size: 14.0)
    header.textLabel?.textAlignment = NSTextAlignment.Right

}

【讨论】:

    【解决方案3】:

    我不确定我是否理解这种情况,但我假设您只是希望标签右对齐而不是左对齐。

    调整标题视图的框架值将不起作用,因为 tableView 负责定位它,它将忽略您在此处设置的任何值。

    您唯一的选择是使用 headerView 的子视图。

    例如,将您的 headerLabel 位置设置为向右对齐:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        static UIView *headerView;
    
        if (headerView != nil)
            return headerView;
    
        NSString *headerText = NSLocalizedString(@"البحث الأخيرة", nil);
    
        float headerWidth = 320.0f;
        float padding = 10.0f;
    
        headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)];
        headerView.autoresizesSubviews = YES;
    
        // create the label centered in the container, then set the appropriate autoresize mask
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerWidth, 44.0f)];
        headerLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
        headerLabel.textAlignment = UITextAlignmentRight;
        headerLabel.text = headerText;
    
        CGSize textSize = [headerText sizeWithFont:headerLabel.font constrainedToSize:headerLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
        headerLabel.frame = CGRectMake(headerWidth - (textSize.width + padding), headerLabel.frame.origin.y, textSize.width, headerLabel.frame.size.height);
    
        [headerView addSubview:headerLabel];
    
        return headerView;
    }
    

    【讨论】:

      【解决方案4】:

      首先我不会用

       headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)];
      

      由于您的原点 x 坐标将为 300。对于页眉和页脚视图,请将您的原点坐标设置为 CGPoint (0.0) 并调整大小。

      尝试使标签与视图本身一样大,并将其对齐设置为正确。

      UIView *headerTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
      
      UILabel *sectionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerTitleView.frame.size.width, 40)];
      sectionTitleLabel.backgroundColor = [UIColor clearColor];
      sectionTitleLabel.textAlignment = UITextAlignmentright;
      

      另一种可能性是在标题视图右侧的某处添加一个 UIlabel 并居中对齐。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-10
        • 2013-03-08
        • 2021-06-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多