开发者社区 问答 正文

添加textField到tableviewheader

需要给标题视图添加一个textField,但是不知道为什么代码执行之后无法看见textField。在使用label的时候正常。

代码:

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

展开
收起
爵霸 2016-03-24 11:50:26 1929 分享 版权
1 条回答
写回答
取消 提交回答
  •  - (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;
        }
    2019-07-17 19:12:32
    赞同 展开评论
问答地址: