Using a XIB file for custom Tableview Section Header

亡梦爱人 提交于 2019-12-14 03:39:41

问题


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

SectionHeaderView.xib is a UIView with a UILabel

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

and in my 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:

header.sectionHeader.text = headerText;

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


回答1:


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;



回答2:


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.

  • 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 CustomTableViewHeaderCell from 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!).

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

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

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

2) Override viewForHeaderInSection to display your custom cell header view

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



回答3:


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

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

2) Instead of using IBOutlet property, Set Tag of UILabel in view (say 101).

Discard SectionHeaderview class.

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

use following code ins Viewforheader method of MasterViewController class:

{
    UIViewController *vc=[[UIViewController alloc] initWithNibName:@"SectionHeaderview" bundle:nil]

    UILable *lblTitle =[vc.view viewWithTag:101];

    lblTitle.text =@"Text you want to set";

    return vc.view;
}


来源:https://stackoverflow.com/questions/12556750/using-a-xib-file-for-custom-tableview-section-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!