xcode 将 XIB 文件用于自定义 Tableview 部分标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12556750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:35:42  来源:igfitidea点击:

Using a XIB file for custom Tableview Section Header

objective-cxcodeuitableviewstoryboardxib

提问by Ali

I wanted to use a xib file to customise a tableview section in xcode (objective C), and here ar my files:

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

SectionHeaderView.xib is a UIView with a UILabel

SectionHeaderView.xib 是一个带有 UILabel 的 UIView

SectionHeaderView.m

SectionHeaderView.m

#import "SectionHeaderView.h"

@implementation SectionHeaderView

@synthesize sectionHeader;

@end

SectionHeaderView.h

SectionHeaderView.h

#import <UIKit/UIKit.h>

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

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

@end

and in my MasterViewController.m

并在我的 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;

}

It works ok till here, however as soon as I set XIB file owner's custom class to "SectionHeaderView" and connect the Label to "sectionHeader" I will get the error "NSUnknownKeyException". I wanted to connect these so I could change the label.text by the following code before returning the haeder:

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

header.sectionHeader.text = headerText;

I am using storyboard (xcode 4.5) for the MasterViewController. Would appreciate any help

我正在为 MasterViewController 使用故事板(xcode 4.5)。将不胜感激任何帮助

回答by andreacipriani

You can create a UITableViewCell subclass with an associated xib, and use it as the section header. In this example i will call it CustomTableViewHeaderCell.h/.m/.xib and show you how to change the text of a label inside this cell.

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

  • Create an outlet property in your CustomTableViewHeaderCell.h

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

  • Add a UITableViewCell into the empty CustomTableViewHeaderCell.xib and set the class of the element to CustomTableViewHeaderCellfrom the Identity Inspector.

  • Set also the Identifier (attribute inspector of the cell) for example CustomIdentifier.

  • Drag a label into the Content View and connect the outlet from the CustomTableViewHeaderCell(Not the file owner!).

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

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

  • 将 UITableViewCell 添加到空的 CustomTableViewHeaderCell.xib 中,并 从 Identity Inspector中将元素的类设置为CustomTableViewHeaderCell

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

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

Then in each ViewController you want to use the table view section header cell:

然后在每个 ViewController 中您要使用表视图部分标题单元格:

1) Register your xib to reuse identifier (probably in viewDidLoad):

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

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

2) Override viewForHeaderInSection to display your custom cell header view

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

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

回答by user1113101

Try this: I have tested it in my app and its working:

试试这个:我已经在我的应用程序中测试过它并且它的工作原理:

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;

回答by user1113101

You can solve this issue by one of the following way:

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

1) you have derived SectionHeaderView from UIView. derive this class with UIViewControllerinstead. This will resolve your issue.

1) 您已经从UIView. 用UIViewController代替派生这个类。这将解决您的问题。

2) Instead of using IBOutletproperty, Set Tag of UILabelin view (say 101).

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

Discard SectionHeaderview class.

丢弃 SectionHeaderview 类。

Keep SectionHeaderView.XIB, delete .m and .h files only.

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

use following code ins Viewforheader method of MasterViewController class:

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