2016-08-16 3 views
0

Ich habe zwei Probleme mit UISearchController. ich dieses Setup verwenden für UISearchController:Swift UISearchController wenn Element suchen Suchleiste ist immer noch

var resultSearchController = UISearchController(searchResultsController: nil) 

override func viewDidLoad() { 
    super.viewDidLoad() 

    resultSearchController = UISearchController(searchResultsController: nil) 
    resultSearchController.searchResultsUpdater = self 
    resultSearchController.dimsBackgroundDuringPresentation = false 
    resultSearchController.hidesNavigationBarDuringPresentation = false 
    resultSearchController.searchBar.placeholder = "Traži.." 
    resultSearchController.searchBar.sizeToFit() 
    tableView.tableHeaderView = resultSearchController.searchBar 

    refreshControl = UIRefreshControl() 
    refreshControl!.attributedTitle = NSAttributedString(string: "Učitaj podatke..") 
    refreshControl!.backgroundColor = UIColor(red:1, green:0.48, blue:0, alpha:0.3) 
    refreshControl!.tintColor = UIColor.whiteColor() 
    refreshControl!.addTarget(self, action: #selector(ArticleTableViewController.getArticleData), forControlEvents: UIControlEvents.ValueChanged) 
    tableView.addSubview(refreshControl!) 
} 

Erstes Problem

wenn ich auf Suchleiste Textfeld, UIRefreshControl zeigt aber es sollte nicht:

enter image description here

Zweite Problem

Wenn ich nach einem Element suche und darauf klicke, benutze ich das Storyboard-Segment, um das Element im neuen Viewcontroller zu öffnen, aber das Problem ist, dass die Suchleiste immer noch auf dem neuen VC vorhanden ist (das erste Bild sucht, das zweite Bild zeigt das neue VC geöffnet)):

enter image description here enter image description here

+0

Sie die Suchleiste Textfeld vor dem Bewegen zum nächsten Bildschirm – Birendra

+0

CLEARE haben - (void) searchBarCancelButtonClicked: (UISearchBar *) SearchBar { searchBar.text = @ ""; } – Birendra

+0

Aber dies ist nicht Abbrechen Schaltfläche, die ich klicke –

Antwort

1

fand ich dieses Lösung:

override func viewDidLoad() { 
    super.viewDidLoad() 

    resultSearchController = UISearchController(searchResultsController: nil) 
    resultSearchController.searchResultsUpdater = self 
    resultSearchController.dimsBackgroundDuringPresentation = false 
    resultSearchController.hidesNavigationBarDuringPresentation = false 
    resultSearchController.searchBar.placeholder = "Traži.." 
    resultSearchController.searchBar.sizeToFit() 
    tableView.tableHeaderView = resultSearchController.searchBar 

    refreshControl = UIRefreshControl() 
    refreshControl!.attributedTitle = NSAttributedString(string: "Učitaj podatke..") 
    refreshControl!.backgroundColor = UIColor(red:1, green:0.48, blue:0, alpha:0.3) 
    refreshControl!.tintColor = UIColor.whiteColor() 
    refreshControl!.addTarget(self, action: #selector(ArticleTableViewController.getArticleData), forControlEvents: UIControlEvents.ValueChanged) 
    tableView.addSubview(refreshControl!) 


    // this hide search bar on next VC, first problem 
    self.definesPresentationContext = true 
    // second problem 
    tableView.contentOffset = CGPointMake(0, CGRectGetHeight(resultSearchController.searchBar.frame)); 

} 
0

sein Mögen Sie einen UISearchBar anstelle des UISearchController verwenden sollte? Versuchen Sie diesen Code:

ViewController.swift

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate { 

@IBOutlet var searchBar: UISearchBar! 
@IBOutlet var tableView: UITableView! 


override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    tableView.dataSource = self 
    tableView.delegate = self 
    searchBar.delegate = self 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 40 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") 
    cell?.textLabel?.text = "\(indexPath.row)" 
    return cell! 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let newViewController = UIViewController() 
    newViewController.view.backgroundColor = UIColor.whiteColor() 
    newViewController.title = "\(indexPath.row)" 
    navigationController?.pushViewController(newViewController, animated: true) 
} 


func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { 
    NSLog(searchText) 
} 
} 

Main.storyboard

enter image description here

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15G31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="u8Q-4a-0UD"> 
<dependencies> 
    <deployment identifier="iOS"/> 
    <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/> 
</dependencies> 
<scenes> 
    <!--View Controller--> 
    <scene sceneID="tne-QT-ifu"> 
     <objects> 
      <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="stackoverflow_38971807" customModuleProvider="target" sceneMemberID="viewController"> 
       <layoutGuides> 
        <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/> 
        <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/> 
       </layoutGuides> 
       <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC"> 
        <rect key="frame" x="0.0" y="0.0" width="600" height="600"/> 
        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> 
        <subviews> 
         <searchBar contentMode="redraw" translatesAutoresizingMaskIntoConstraints="NO" id="GdH-fg-5TJ"> 
          <rect key="frame" x="0.0" y="64" width="600" height="44"/> 
          <textInputTraits key="textInputTraits"/> 
         </searchBar> 
         <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="tP2-Xq-aem"> 
          <rect key="frame" x="0.0" y="108" width="600" height="492"/> 
          <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> 
          <prototypes> 
           <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="cell" textLabel="RcZ-LI-9Ir" style="IBUITableViewCellStyleDefault" id="13y-5Y-dcw"> 
            <rect key="frame" x="0.0" y="28" width="600" height="44"/> 
            <autoresizingMask key="autoresizingMask"/> 
            <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="13y-5Y-dcw" id="ldM-4e-kie"> 
             <rect key="frame" x="0.0" y="0.0" width="600" height="43"/> 
             <autoresizingMask key="autoresizingMask"/> 
             <subviews> 
              <label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="RcZ-LI-9Ir"> 
               <rect key="frame" x="15" y="0.0" width="570" height="43"/> 
               <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> 
               <fontDescription key="fontDescription" type="system" pointSize="16"/> 
               <color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/> 
               <nil key="highlightedColor"/> 
              </label> 
             </subviews> 
            </tableViewCellContentView> 
           </tableViewCell> 
          </prototypes> 
         </tableView> 
        </subviews> 
        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> 
        <constraints> 
         <constraint firstItem="GdH-fg-5TJ" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="Fwk-Jx-EJc"/> 
         <constraint firstAttribute="trailing" secondItem="GdH-fg-5TJ" secondAttribute="trailing" id="Q3j-AV-Tnj"/> 
         <constraint firstItem="tP2-Xq-aem" firstAttribute="bottom" secondItem="wfy-db-euE" secondAttribute="top" id="T9u-C9-1HD"/> 
         <constraint firstItem="GdH-fg-5TJ" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="Y9o-yO-w6R"/> 
         <constraint firstItem="GdH-fg-5TJ" firstAttribute="leading" secondItem="tP2-Xq-aem" secondAttribute="leading" id="l4X-RD-XEW"/> 
         <constraint firstItem="GdH-fg-5TJ" firstAttribute="trailing" secondItem="tP2-Xq-aem" secondAttribute="trailing" id="pcF-ST-Zkq"/> 
         <constraint firstItem="tP2-Xq-aem" firstAttribute="top" secondItem="GdH-fg-5TJ" secondAttribute="bottom" id="ze4-D7-NeF"/> 
        </constraints> 
       </view> 
       <navigationItem key="navigationItem" id="pJE-Q4-d47"/> 
       <connections> 
        <outlet property="searchBar" destination="GdH-fg-5TJ" id="N81-72-8HH"/> 
        <outlet property="tableView" destination="tP2-Xq-aem" id="lYV-er-UNj"/> 
       </connections> 
      </viewController> 
      <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/> 
     </objects> 
     <point key="canvasLocation" x="1075" y="-133"/> 
    </scene> 
    <!--Navigation Controller--> 
    <scene sceneID="Q5J-kb-qjE"> 
     <objects> 
      <navigationController automaticallyAdjustsScrollViewInsets="NO" id="u8Q-4a-0UD" sceneMemberID="viewController"> 
       <toolbarItems/> 
       <navigationBar key="navigationBar" contentMode="scaleToFill" id="ey2-Nm-1yZ"> 
        <rect key="frame" x="0.0" y="0.0" width="320" height="44"/> 
        <autoresizingMask key="autoresizingMask"/> 
       </navigationBar> 
       <nil name="viewControllers"/> 
       <connections> 
        <segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="A3N-1a-Rlv"/> 
       </connections> 
      </navigationController> 
      <placeholder placeholderIdentifier="IBFirstResponder" id="QFX-N3-4dT" userLabel="First Responder" sceneMemberID="firstResponder"/> 
     </objects> 
     <point key="canvasLocation" x="218" y="-133"/> 
    </scene> 
</scenes> 
</document> 
+0

Und wenn ich zurück gehe, muss ich diese Suchleiste wieder anzeigen? –

+0

Ja. Auch in tableView (_: didSelectRowAt :) können Sie UISearchController auf Ihren neuen viewController ("vc" im obigen Code) setzen. Und dann solltest du func viewWillDisappear (animiert: Bool) {resultSearchController.searchBar.hidden = false} deines vc überschreiben. –

+0

Aber wenn ich 'viewDidApear' verwende und' hidden' auf 'false' stelle, ist es langsam und die Suche filtert nicht? –

Verwandte Themen