1

ヘッダービューにテキストフィールドを追加しようとしています。テキストフィールドが表示されない理由がわかりませんでした。ラベルを使用すると、完全に機能します。

コードは次のとおりです。

-(UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
       UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(40, 0, self.view.frame.size.width - 70, 30)];
    UITextField *sectionTitleTF1 = [[UITextField alloc] initWithFrame:CGRectMake(58, 0, 500, 30)];
    sectionTitleTF1.backgroundColor = [UIColor whiteColor];
    [sectionTitleTF1 becomeFirstResponder];
    [tableHeaderView addSubview:sectionTitleTF1];
return tableHeaderView;

}

ありがとう

4

3 に答える 3

0

データソースのこのメソッドを実装しましたか?セクションヘッダーを表示する場合は必須です。 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30.0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

また、気付いていない場合はUITextField、サブビューとして追加するのではなく、直接返すことができます。ただし、達成したいことによっては、サブビューとして使用することもできます。

編集:データソースは委任されません

于 2013-02-22T23:37:34.683 に答える
0

これを試して、

  You can adjust view, textfield frame based on your device ipad or iphone




 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30.0;
}

-(UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(40, 0, self.view.frame.size.width - 70, 30)];
tableHeaderView.backgroundColor =[UIColor grayColor];
UITextField *sectionTitleTF1 = [[UITextField alloc] initWithFrame:CGRectMake(58, 0, 500, 30)];
sectionTitleTF1.backgroundColor = [UIColor whiteColor];
[sectionTitleTF1 setBackgroundColor:[UIColor whiteColor]];
[sectionTitleTF1 setFont:[UIFont boldSystemFontOfSize:15]];
[sectionTitleTF1 setBorderStyle:UITextBorderStyleLine];
[sectionTitleTF1 setTextAlignment:UITextAlignmentCenter];
[sectionTitleTF1 setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[sectionTitleTF1 becomeFirstResponder];
[tableHeaderView addSubview:sectionTitleTF1];
return tableHeaderView;
}
于 2013-02-23T03:40:25.593 に答える
0

これを追加するだけ sectionTitleTF1.borderStyle = UITextBorderStyleRoundedRect; で表示されます

于 2014-10-29T09:11:15.733 に答える