8

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) を使用しています。助けていただければ幸いです

4

3 に答える 3

15

関連する xib を使用して UITableViewCell サブクラスを作成し、それをセクション ヘッダーとして使用できます。この例では、それをCustomTableViewHeaderCell .h/.m/.xib と呼び、このセル内のラベルのテキストを変更する方法を示します。

  • CustomTableViewHeaderCell.h にアウトレット プロパティを作成します。

    @property (弱い、非アトミック) IBOutlet UILabel *sectionHeaderLabel;

  • UITableViewCell を空の CustomTableViewHeaderCell.xib に追加し、要素のクラスを Identity Inspector からCustomTableViewHeaderCellに設定します。

  • CustomIdentifierなどの識別子 (セルの属性インスペクター) も設定します 。

  • ラベルをコンテンツ ビューにドラッグし、 CustomTableViewHeaderCell (ファイルの所有者ではありません!) からアウトレットを接続します。

次に、各 ViewController で、テーブル ビュー セクション ヘッダー セルを使用します。

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;
}
于 2014-09-16T12:40:10.170 に答える
2

この問題は、次のいずれかの方法で解決できます。

1)からSectionHeaderViewを派生させましたUIView。代わりにこのクラスを派生さ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;
}
于 2012-09-24T07:09:50.167 に答える