【问题标题】:Adding iOS UITableView HeaderView (not section header)添加iOS UITableView HeaderView(不是节标题)
【发布时间】:2011-07-23 10:51:32
【问题描述】:

我想在联系人应用程序中添加表格标题(而不是部分标题),例如:

完全一样 - 表格上方图像旁边的标签。

我希望所有视图都是可滚动的,所以我不能将它们放在表格之外。

我该怎么做?

【问题讨论】:

    标签: iphone ios uitableview


    【解决方案1】:

    Swift 中:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // We set the table view header.
        let cellTableViewHeader = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewHeaderCustomCellIdentifier) as! UITableViewCell
        cellTableViewHeader.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewHeaderCustomCellIdentifier]!)
        self.tableView.tableHeaderView = cellTableViewHeader
    
        // We set the table view footer, just know that it will also remove extra cells from tableview.
        let cellTableViewFooter = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewFooterCustomCellIdentifier) as! UITableViewCell
        cellTableViewFooter.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewFooterCustomCellIdentifier]!)
        self.tableView.tableFooterView = cellTableViewFooter
    }
    

    【讨论】:

    • 在 iOS 7 或更高版本上使用这种方法会触发运行时警告:“没有重复使用表格单元格的索引路径”。为避免此错误,请参考link
    【解决方案2】:
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
        {
    
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
        headerView.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
        headerView.layer.borderColor=[UIColor blackColor].CGColor;
        headerView.layer.borderWidth=1.0f;
    
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5,100,20)];
    
        headerLabel.textAlignment = NSTextAlignmentRight;
        headerLabel.text = @"LeadCode ";
        //headerLabel.textColor=[UIColor whiteColor];
        headerLabel.backgroundColor = [UIColor clearColor];
    
    
        [headerView addSubview:headerLabel];
    
        UILabel *headerLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];
    
        headerLabel1.textAlignment = NSTextAlignmentRight;
        headerLabel1.text = @"LeadName";
        headerLabel.textColor=[UIColor whiteColor];
        headerLabel1.backgroundColor = [UIColor clearColor];
    
        [headerView addSubview:headerLabel1];
    
        return headerView;
    
    }
    

    【讨论】:

    • 不明白你的问题?
    • 这是错误的。他只想要一个表头而不是节头。这两件事是非常不同的。请阅读问题。
    【解决方案3】:

    您也可以在界面构建器中仅创建一个 UIView,然后拖放 ImageView 和 UILabel(使其看起来像您想要的标题),然后使用它。

    一旦您的 UIView 看起来也符合您的要求,您可以通过 XIB 以编程方式对其进行初始化并添加到您的 UITableView。换句话说,您不必在 IB 中设计 ENTIRE 表。只是headerView(这样header view也可以在其他表中复用)

    例如,我的一个表头有一个自定义 UIView。该视图由名为“CustomHeaderView”的 xib 文件管理,并使用我的 UITableViewController 子类中的以下代码将其加载到表头中:

    -(UIView *) customHeaderView {
        if (!customHeaderView) {
            [[NSBundle mainBundle] loadNibNamed:@"CustomHeaderView" owner:self options:nil];
        }
    
        return customHeaderView;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // Set the CustomerHeaderView as the tables header view 
        self.tableView.tableHeaderView = self.customHeaderView;
    }
    

    【讨论】:

      【解决方案4】:

      UITableView 有一个 tableHeaderView 属性。将其设置为您想要的任何视图。

      使用新的UIView 作为容器,将文本标签和图像视图添加到新的UIView,然后将tableHeaderView 设置为新视图。

      例如,在UITableViewController

      -(void)viewDidLoad
      {
           // ...
           UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
           UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
           [headerView addSubview:imageView];
           UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
           [headerView addSubview:labelView];
           self.tableView.tableHeaderView = headerView;
           [imageView release];
           [labelView release];
           [headerView release];
           // ...
      } 
      

      【讨论】:

      • 太棒了!谢谢它的工作。是否有任何属性可以使 tableview 单元格与里面的文本一样大?
      • 为了做到这一点,您需要能够计算给定字符串的单元格的高度,并将该值传递给 heightForRowAtIndexPath。您可以使用 NSString 的方法 sizeWithFont:constrainedToSize: 函数来计算文本在限制为大小时的高度。
      • 布局后添加,否则大小将不是你想要的。而且 viewDidLoad 是个好地方。
      • 在 viewDidLoad 阶段设置视图的几何形状是个好习惯吗?
      • 好的,如果您在表格源代码中设置标题视图,它似乎可以工作,请确保也设置高度:例如:public override UIView GetViewForHeader(UITableView tableView, nint section) { return headerView; } public override nfloat GetHeightForHeader(UITableView tableView, nint section) { return headerView.Frame.Height; }
      【解决方案5】:

      您可以在 Interface Builder 中轻松完成。只需创建一个带有表的视图并将另一个视图拖放到表上。这将成为表头视图。将您的标签和图像添加到该视图。有关视图层次结构,请参见下图。

      【讨论】:

      • 我更喜欢这个答案,因为当我可以整齐地设置代码并将其放置在 IB 中时,为什么还要编写代码
      • 非常感谢您的提示。顺便说一句,如果其他人想知道或尝试这样做,您不需要在标题中添加滚动视图。它为您的 Header 使用 UITableView 的 ScrollView,因为 Header 在技术上是 UITableView 的一部分
      • 值得一提的是它现在在 StoryBoard 编辑器中的工作方式:stackoverflow.com/q/7841167/926907
      • 不要为此使用笔尖。加载时间更长。用代码写出来。为了可重用性,创建一个自定义类并实例化...
      • 它不会让我在标题视图中添加高度约束。
      猜你喜欢
      • 2013-09-21
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 2011-07-23
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多