iphone - 右侧的 UITableView 部分标题而不是左侧的默认值

标签 iphone objective-c ios 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;
}

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

我需要标题在右边。

最佳答案

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

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

关于iphone - 右侧的 UITableView 部分标题而不是左侧的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12672231/

相关文章:

ios - 从自定义 UITableViewCell 添加和删除自定义 View

java - 使用j2objc将android程序转换为iOS:找不到某些 header

ios - 使用大括号或标准+ dictionaryWithObjectsAndKeys初始化NSDictionary之间有什么区别?

ios - 如何更新 Fabric 和 Crashlytics?

ios - 标签打印可选()即使强制展开

iOS模拟器: Which folders can be safely deleted?

iphone - 渲染重叠的半透明对象而不使重叠变暗

ios - 将数据从 tableview 传回 Viewcontroller

iphone - ios 6.0 中的方向问题

objective-c - cocoa :为什么事件没有进行?