Search code examples
iphoneiosipadios6

add textfield to tableviewheader


I am trying to add textfield to header view. I could not figure out why I can't see my textfield. When I use a label it works perfectly.

Following is the code:

-(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;

}

Thanks


Solution

  • Try this,

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