2012-10-01 75 views
12

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

我搜索,很多爱好者建议实施:

- (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坐标,它不会影响视图的位置。它始终位于桌子的中央。

我需要标题在右侧。

+0

而是用flexibleWidth去的,你不能强迫它是全宽?然后视图将消耗完整的宽度并证明文本是正确的。 – bryanmac

+0

您已经选择了一个大多数情况下正确的答案,但添加空格来获得保证金是一个真正的破解。解决你的问题的正确方法是创建一个空的视图(或一个背景颜色)并将其用作headerView。然后你添加一个UILabel到该容器视图,并从右侧偏移它。这个概念 - 使用空视图作为容器来放置其他视图是一种常用且有用的工具。 –

+0

@DavidH你是对的。增加空格的想法并不好。虽然它暂时对我有效。正如我在我的问题中提到的那样,观点总是在中心位置,因为我需要它在右边。设置框架不起作用。此外,该标签是右对齐的。我明白使用UIView有更多的自定义选项。感谢您的评论! –

回答

15

这为我工作,但与框架的坐标玩根据您的需要对齐

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

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

耶正确假设UILabel是uiview的子类 –

+1

谢谢!有效 :)。然而,文本的开始几乎触及视图边界。我在文本中添加了一些空格,现在文本稍微偏向左侧。 –

+3

'UITextAlignmentRight'已弃用,应使用'NSTextAlignmentRight' –

0

firt所有的我不会

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

初始化UIView的由于您的原点x坐标将是300.For头和footerviews设定原点坐标,以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标题视图右侧的某个地方与中心对齐。

0

我不知道我了解的情况,但我假设你只是想标签对齐而不是左对齐。

调整标题视图的框架值将不起作用,因为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; 
} 
+0

对齐方式已设置为正确。 –

7

我有麻烦改变与上述解决方案的文本,这为我工作的地方有适当的间隔距离的后缘表视图。在斯威夫特

UITableViewDataSource

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

的UITableViewDelegate PS解决方案

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 

} 
相关问题