【问题标题】:Using a XIB file for custom Tableview Section Header将 XIB 文件用于自定义 Tableview 部分标题
【发布时间】:2012-09-15 10:10:33
【问题描述】:

我想使用 xib 文件在 xcode(目标 C)中自定义 tableview 部分,这是我的文件:

SectionHeaderView.xib 是一个带有 UILabel 的 UIView

SectionHeaderView.m

#import "SectionHeaderView.h"

@implementation SectionHeaderView

@synthesize sectionHeader;

@end

SectionHeaderView.h

#import <UIKit/UIKit.h>

@interface SectionHeaderView : UIView
{
IBOutlet UILabel *sectionHeader;
}

@property (nonatomic, strong) IBOutlet UILabel *sectionHeader;

@end

在我的 MasterViewController.m 中

#import "SectionHeaderView.h"

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

SectionHeaderView  *header = [[[NSBundle mainBundle] loadNibNamed:@"SectionHeaderView" owner:self options:nil] objectAtIndex:0];

return header;

}

到这里为止一切正常,但是一旦我将 XIB 文件所有者的自定义类设置为“SectionHeaderView”并将标签连接到“sectionHeader”,我将收到错误“NSUnknownKeyException”。我想连接这些,所以我可以在返回 haeder 之前通过以下代码更改 label.text:

header.sectionHeader.text = headerText;

我正在为 MasterViewController 使用情节提要 (xcode 4.5)。 希望有任何帮助

【问题讨论】:

    标签: objective-c xcode uitableview storyboard xib


    【解决方案1】:

    您可以通过以下方式之一解决此问题:

    1) 您从UIView 派生了 SectionHeaderView。用UIViewController 派生这个类。这将解决您的问题。

    2) 不使用IBOutlet 属性,而是在视图中设置UILabel 的标记(比如101)。

    丢弃 SectionHeaderview 类。

    保留 SectionHeaderView.XIB,仅删除 .m 和 .h 文件。

    在 MasterViewController 类的 Viewforheader 方法中使用以下代码:

    {
        UIViewController *vc=[[UIViewController alloc] initWithNibName:@"SectionHeaderview" bundle:nil]
    
        UILable *lblTitle =[vc.view viewWithTag:101];
    
        lblTitle.text =@"Text you want to set";
    
        return vc.view;
    }
    

    【讨论】:

    • 我尝试了第二种方法并得到一个错误:'-[UIViewController _loadViewFromNibNamed:bundle:] 加载了“SectionFooterView”笔尖,但未设置视图出口。'
    • 试试这个:我已经在我的应用程序中测试了它并且它的工作:'code' NSArray *arrviw= [[NSBundle mainBundle] loadNibNamed:@"SectionHeaderview" owner:self options:nil]; UIView *view = [arrviw objectAtIndex:0]; UILable *lblTitle =[查看 viewWithTag:101]; lblTitle.text =@"你要设置的文字";返回视图;
    • 将 UIViewController 设置为另一个 UIViewController 的标头的优点是什么?标头实际上是一个 UIView。最初的问题是如何将 nib 文件加载为 header。
    【解决方案2】:

    试试这个:我已经在我的应用中测试了它并且它的工作:

    NSArray *viewArray =  [[NSBundle mainBundle] loadNibNamed:@"SectionHeaderview" owner:self options:nil];  
    UIView *view = [viewArray objectAtIndex:0]; 
    UILabel *lblTitle = [view viewWithTag:101]; 
    lblTitle.text = @"Text you want to set"; 
    return view;
    

    【讨论】:

      【解决方案3】:

      您可以创建一个带有关联 xib 的 UITableViewCell 子类,并将其用作节标题。在本例中,我将其命名为 CustomTableViewHeaderCell.h/.m/.xib 并向您展示如何更改此单元格内标签的文本。

      • 在您的 CustomTableViewHeaderCell.h 中创建一个 outlet 属性

        @property (weak, nonatomic) IBOutlet UILabel *sectionHeaderLabel;

      • 将 UITableViewCell 添加到空的 CustomTableViewHeaderCell.xib 并将元素的类设置为 CustomTableViewHeaderCell 来自身份检查员。

      • 例如还设置标识符(单元格的属性检查器) 自定义标识符

      • 将标签拖入内容视图并从 CustomTableViewHeaderCell(不是文件所有者!)。

      然后在每个ViewController中你要使用table view section header cell:

      1) 注册您的 xib 以重用标识符(可能在 viewDidLoad 中):

      [_yourTableView registerNib:[UINib nibWithNibName:@"CustomTableViewHeader" bundle:nil] forCellReuseIdentifier:@"CustomIdentifier"];
      

      2) 覆盖 viewForHeaderInSection 以显示您的自定义单元格标题视图

      -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
          CustomTableViewHeaderCell * customHeaderCell = [tableView dequeueReusableCellWithIdentifier:@"CustomIdentifier"];
          customHeaderCell.sectionHeaderLabel = @"What you want";
          return customHeaderCell;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-26
        相关资源
        最近更新 更多