Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 将搜索栏和搜索显示控制器添加到复杂的tableHeaderView时,UISearchBar在获得焦点时会消失_Swift_Ios8_Uisearchbar_Uisearchdisplaycontroller - Fatal编程技术网

Swift 将搜索栏和搜索显示控制器添加到复杂的tableHeaderView时,UISearchBar在获得焦点时会消失

Swift 将搜索栏和搜索显示控制器添加到复杂的tableHeaderView时,UISearchBar在获得焦点时会消失,swift,ios8,uisearchbar,uisearchdisplaycontroller,Swift,Ios8,Uisearchbar,Uisearchdisplaycontroller,我使用的是一个ViewController和一个相关的NIB,没有故事板。我准备把头发拔出来 问题 请注意,在对焦后的视频中,左上角可以看到一小块区域,单击UITableView上的阴影区域后,该区域将完全消失。进入视图层次结构时,它显示搜索栏正在移出其原始层次结构位置 笔尖 代码 导入UIKit 类ViewController:UIViewController、UITableViewDataSource、, UITableViewDelegate、UISearchDisplayDelega

我使用的是一个ViewController和一个相关的NIB,没有故事板。我准备把头发拔出来

问题 请注意,在对焦后的视频中,左上角可以看到一小块区域,单击UITableView上的阴影区域后,该区域将完全消失。进入视图层次结构时,它显示搜索栏正在移出其原始层次结构位置

笔尖

代码
导入UIKit
类ViewController:UIViewController、UITableViewDataSource、,
UITableViewDelegate、UISearchDisplayDelegate、UISearchBarDelegate{
@IBVAR弱变量bigLabel:UILabel!
@IBOutlet弱var按钮组段:UISegmentedControl!
@IBOutlet var tableViewHeader:UIView!
@IBVAR表格视图:UITableView!
@iBearchBar:UISearchBar!
//标记:视图控制器
重写init(){
super.init(nibName:“ViewController”,bundle:nil)
}
必需的初始化(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
}
覆盖功能视图将出现(动画:Bool){
超级。视图将显示(动画)
self.initTableView();
}
重写func viewDidLoad(){
super.viewDidLoad();
self.initLabelStyling();
//加载视图后,通常从nib执行任何其他设置。
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
//标记:表视图标题
func initTableViewHeader()->Void{
self.tableView.tableHeaderView=self.tableViewHeader;
self.initSearchView();
self.initSegmentedControl();
}
//MARK:分段控件
func initSegmentedControl()->Void{
字体=UIFont(名称:“HelveticaNeue”,大小:10);
让color=UIColor.whiteColor();
让lighterColor=color.colorWithAlphaComponent(0.5);
self.buttonGroupSegment.tintColor=UIColor.clearColor();
self.buttonGroupSegment.setTitleTextAttributes([NSForegroundColorAttributeName:lighterColor,NSFontAttributeName:font!,NSKernAttributeName:0.7],用于状态:UIControlState.Normal)
self.buttonGroupSegment.setTitleTextAttributes([NSForegroundColorAttributeName:color,NSFontAttributeName:font!],forState:uicontrol状态。选中);
//更改所有标题的大小写
设c=self.buttonGroupSegment.subviews.count;
因为我在虚空中{
让searchBar=self.searchDisplayController!.searchBar;
searchBar.backgroundColor=UIColor.clearColor();
searchBar.backgroundImage=UIImage();
searchBar.barTintColor=UIColor.clearColor();
searchBar.tintColor=UIColor.whiteColor()
让numViews=searchBar.subviews[0].subviews.count;
var searchField:UITextField?
因为我在0..Bool{
self.filterContentForSearchText(searchString,作用域:“”);
返回true;
}
func filterContentForSearchText(searchText:NSString,作用域:String)->Void{
self.dataFiltered=self.data.filter({(pet:String)->Bool-in
让stringMatch=pet.rangeOfString(searchText)
返回字符串匹配!=nil;
})
}
//标记:表视图
让数据=[“猫”、“狗”、“鸟”、“鱼”、“雪貂”、“老鼠”、“仓鼠”、“鸡”、“山羊”、“猪”、“驴”、“猴子”、“兔子”、“狐狸”、“蛇”、“青蛙”、“蜘蛛”、“公鸡”、“鳄鱼”、“波库平”、“松鼠”、“鸭子”、“海龟”、“蜥蜴”、“喇嘛”];
var dataFiltered:[字符串]=[];
让cellClass:String=“Cell”;
func initTableView()->Void{
self.initTableViewHeader();
self.tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier:self.cellClass);
//标记:表视图委托和数据源方法
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
如果tableView==self.searchDisplayController!.searchResultsTableView{
返回self.dataFiltered.count
}否则{
返回self.data.count;
}
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
变量单元格:UITableViewCell?
cell=self.tableView.dequeueReusableCellWithIdentifier(cellClass)为?UITableViewCell;
如果(单元格==nil){
cell=UITableViewCell(样式:UITableViewCellStyle.Default,reuseIdentifier:self.cellClass)
}
如果tableView==self.searchDisplayController!.searchResultsTableView{
单元格?.textLabel?.text=self.dataFiltered[indexPath.row]
}否则{
单元格?.textLabel?.text=self.data[indexPath.row];
}
返回单元!;
}
//标记:设置标签样式(添加线)
func initLabelStyling()->Void{
设v=UIView(frame:CGRectMake(0,0,self.biglab.frame.width,1));
v、 backgroundColor=UIColor(红色:100,绿色:100,蓝色:100,阿尔法:0.2);
self.bigLabel.addSubview(v)
}
}

这个问题解决了吗?我知道这与导航控制器中的导航栏被覆盖在搜索栏顶部有关。如果你在iPhone 6模拟器中运行它,然后将它转到横向,你可以看到导航栏下的搜索栏。如果我有解决方案,将报告返回n

编辑:好了,明白了

我添加了以下内容(setNavigationBarHidden),可能还有更好的地方,但因为我只是在进行原型设计,现在就可以了:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.searchDisplayController!.active {
        self.navigationController?.setNavigationBarHidden(true, animated: true)
        return self.filteredPlayers.count
    } else {
        return self.selectedPlayers.count
    }
}

...

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if self.searchDisplayController!.active {
        self.selectedPlayers.append(self.filteredPlayers[indexPath.row])
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        self.searchDisplayController!.setActive(false, animated: true)
        self.tableView.reloadData()
    }
}

不幸的是,添加这两种方法并没有奏效,搜索字段在获得焦点时仍然“消失”修正了这个问题,通过使用一个香草搜索条,完全删除搜索显示控制器位,因为这个解决方案没有回答我原来的问题,我认为这是一个解决办法,所以我把问题打开了。
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.searchDisplayController!.active {
        self.navigationController?.setNavigationBarHidden(true, animated: true)
        return self.filteredPlayers.count
    } else {
        return self.selectedPlayers.count
    }
}

...

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if self.searchDisplayController!.active {
        self.selectedPlayers.append(self.filteredPlayers[indexPath.row])
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        self.searchDisplayController!.setActive(false, animated: true)
        self.tableView.reloadData()
    }
}