2016-03-21 11 views
0

オートコンプリートでsearchBarを実装しようとしています。入力を開始しても値がない場合は、セル「結果なし」が表示されます。しかし、私が最初にsearchFieldを選択するとき、またはそれからすべての文字を削除すると、セルは消滅します。私はsearchFieldが ""だと表示されるように管理しますが、私が説明したように動作させる方法は見つけられません。オートコンプリートSearchBar - 値のセルではありません。

マイコード:

@IBOutlet weak var searchField: UISearchBar! 
@IBOutlet var autocompleteTableView: UITableView! 

var pastUrls = ["Men", "Women", "Cats", "Dogs", "Children"] 
var autocompleteUrls = [String]() 
var searchActive : Bool = false 

override func viewDidLoad() { 
    super.viewDidLoad() 
    searchField.delegate = self 
    autocompleteTableView.backgroundColor = UIColor.clearColor() 
    autocompleteTableView!.delegate = self 
    autocompleteTableView!.dataSource = self 
    autocompleteTableView!.scrollEnabled = true 
    autocompleteTableView!.hidden = true 
} 


func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { 
    autocompleteTableView.hidden = false 
    let substring = (searchBar.text! as NSString).stringByReplacingCharactersInRange(range, withString: text) 
    searchAutocompleteEntriesWithSubstring(substring) 
    return true 
} 

func searchAutocompleteEntriesWithSubstring(substring: String) { 
    autocompleteUrls.removeAll(keepCapacity: false) 
    for curString in pastUrls { 
     if curString.lowercaseString.rangeOfString(substring) != nil { 
      autocompleteUrls.append(curString) 
     } 
    } 
    autocompleteTableView!.reloadData() 
} 

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

     return autocompleteUrls.count 

} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("cell") 

    if let _ = cell { 
     let index = indexPath.row as Int 
     cell!.textLabel!.text = autocompleteUrls[index] 
    } else { 
     cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell") 
    } 
    return cell! 

} 

答えて

0

あなたの配列が空であり、それはまた、1

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if autocompleteUrls.count != 0 { 
     return autocompleteUrls.count 
    } 

    return 1 

} 

を返している場合は、あなたからDZNEmptyDataSetを使用することができればあなたのnumberOfRowsInSection方法でチェックする必要がありますGitHubこれは表示するデータがない場合は、tableViewに何かを表示する優れたデリゲートメソッドのセットです。

関連する問題