9

これが私がやったことです:

xibカスタム テーブル セクション ヘッダーに使用される小さな UIView を持つカスタム ファイルを作成しました。カスタムxibファイルを分類しました。

これをヘッダーとしてtableViewに追加したい。いくつかのリソースを見てきましたが、それらは古くなっているか、情報が不足しているようです。

ドキュメントを見ると、次の手順でカスタム ヘッダーを追加するための参照が表示されます。

テーブル ビューにヘッダー ビューまたはフッター ビューを認識させるには、それを登録する必要があります。これを行うには、UITableView の registerNib:forCellReuseIdentifier: または registerClass:forCellReuseIdentifier: メソッドを使用します。

ストーリーボード ビューに tableView を追加すると、XCode 内で再利用識別子を簡単に割り当てることができました。カスタム セルxibファイルを作成することもでき、XCode 内で識別子を再利用する場所もありました。

セクション ヘッダーのカスタム UIView を作成したときに、再利用識別子のエントリがありませんでした。これがないと、使い方がわかりませんregisterNib:forCellReuseIdentifier

詳細:tableView内部のある絵コンテ シーンがあります。tableViewはリンクされたカスタム クラスのものであり、オブジェクトtableViewは親ビューのViewControllerファイルにアウトレットを持っています。

親はとViewControllerの両方です。ここでも、問題なくカスタム セルを実装できました。タイトル以外の方法でヘッダーを変更することさえできません。UITableViewDataSourceDelegateUITableViewDelegate

[[self tableHeaderView] setBackgroundColor:[UIColor clearColor]];カスタム クラスからメソッドを呼び出してみましたがtableView、何も起こりません。次のようにアウトレット名を使用して、親ViewControllerクラス でこのメソッドを使用してみました。[[self.tableOutlet tableHeaderView] setBackgroundColor:[UIColor clearColor]];

どんな助けでも大歓迎です。

編集: (背景を透明に変更することはできません)

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

    HeaderView *headerView = [self.TableView dequeueReusableHeaderFooterViewWithIdentifier:@"tableHeader"];

    // Set Background color
    [[headerView contentView] setBackgroundColor:[UIColor clearColor]];

    // Set Text
    headerView.headerLabel.text = [self.sectionArray objectAtIndex:section];

    return headerView;
}
4

2 に答える 2

20

xib に識別子を設定する必要はありません。登録時とヘッダー ビューのデキュー時に同じ識別子を使用するだけで済みます。viewDidLoad メソッドでは、次のようにビューを登録しました。

[self.tableView registerNib:[UINib nibWithNibName:@"Header1" bundle:nil] forHeaderFooterViewReuseIdentifier:@"header1"];

次に、デリゲート メソッドで:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header1"];
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 100;
}
于 2013-01-28T21:43:26.860 に答える
0

On the problem with background color (Unless you want transparent):

You can create an UIView which occupies the whole view then change the background color of that.

If you don't want others to know what's happening, you can overwrite the backgroundColor property:

//interface
@property (copy, nonatomic) UIColor *backgroundColor;


//implementation
@dynamic backgroundColor;

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    //self.viewBackground is the created view
    [self.viewBackground setBackgroundColor:backgroundColor];
}

- (UIColor *)backgroundColor {
    return self.viewBackground.backgroundColor;
}
于 2015-04-15T12:37:09.893 に答える