【问题标题】:Issues setting up searchdisplaycontroller with tableview - Swift使用 tableview 设置 uisearchdisplaycontroller 的问题 - Swift
【发布时间】:2016-05-21 22:37:50
【问题描述】:

我正在尝试为我的 tableview 设置 searchdisplaycontroller,并且在为 tableview 本身声明我的 iboutlet 时遇到了几个问题。附件是屏幕截图以及我的代码。非常感谢任何见解。

Code Errors Img

import UIKit

class DataTableExercisesTableViewController: UITableViewController, UISearchBarDelegate {

var exercises = ["Abs", "Arms", "Back", "Chest", "Legs", "Shoulders", "Triceps"]
var searchActive : Bool = false

@IBOutlet var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var filtered:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.tableFooterView = UIView()

    tableView.delegate = self
    tableView.dataSource = self
    searchBar.delegate = self

}



// MARK: - Table view data source



override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(searchActive) {
        return filtered.count
    }
    return exercises.count;
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell;
    if(searchActive){
        cell.textLabel?.text = filtered[indexPath.row]
    } else {
        cell.textLabel?.text = exercises[indexPath.row];
    }

    return cell;
}




 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {



}


func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    searchActive = true;
}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    searchActive = false;
}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    searchActive = false;
}

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

    filtered = exercises.filter({ (text) -> Bool in
        let tmp: NSString = text
        let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
        return range.location != NSNotFound
    })
    if(filtered.count == 0){
        searchActive = false;
    } else {
        searchActive = true;
    }
    self.tableView.reloadData()
}

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

【问题讨论】:

    标签: ios swift uitableview uisearchdisplaycontroller


    【解决方案1】:

    您是否尝试过完全移除 IBoutlet? (来自故事板连接和您的代码)。我认为这将解决您的问题。

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多