无法在swift中实现UISearchBar

时间:2014-11-06 10:40:00

标签: ios swift

这是主文件的代码,它没有给出任何错误,但它不起作用:

import UIKit


class mainVC: UIViewController,UITableViewDelegate,UITableViewDataSource , UISearchBarDelegate, UISearchDisplayDelegate {


//this is your tableview

@IBOutlet var table: UITableView!
@IBOutlet var selectedMin: NSString!
@IBOutlet var tasksList: NSArray!
@IBOutlet var searchResults :NSArray!


@IBOutlet var searchBar: UISearchBar!


override func viewDidLoad() {
    super.viewDidLoad()



   // navigationItem.titleView = UIImageView(image:UIImage(named: "logo3"))
    navigationItem.title="مجمع الخدمات الحكومية"

    //cell height
    self.table.rowHeight = 50
    //tableview background
    self.table.backgroundView = UIImageView(image: UIImage(named: "space.jpg"))

    if let path = NSBundle.mainBundle().pathForResource(selectedMin, ofType: "plist")
        {
            tasksList = NSArray(contentsOfFile: path)

        }

    table.separatorColor = UIColor.yellowColor()
}


func filterContentForSearchText(searchText:NSString , scope: String = "All")
{
    //var searchText = ""

    var resultPredicate : NSPredicate = NSPredicate(format: "name contains[c] %@", searchText)

    //var recipes : NSArray = NSArray()

    var searchResults = self.tasksList.filteredArrayUsingPredicate(resultPredicate)
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
    let scopes = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
    let selectedScope = scopes[self.searchDisplayController!.searchBar.selectedScopeButtonIndex] as String
    self.filterContentForSearchText(searchString, scope: selectedScope)
    return true
}

func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
    let scope = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
    self.filterContentForSearchText(self.searchDisplayController!.searchBar.text, scope: scope[searchOption])
    return true
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()


}

override func viewWillAppear(animated: Bool) {
    table.reloadData()
}

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

    if table == self.searchDisplayController!.searchResultsTableView {
        return self.searchResults.count
    } else{
        return tasksList.count}

}

func tableView(tableView :UITableView,cellForRowAtIndexPath indexpath:NSIndexPath)->UITableViewCell

{
    var dict :NSDictionary!
    let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "TableView")

    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

    cell.textLabel?.textColor=UIColor.whiteColor()
    //assigning the content of var items to the textlabel of each cell

    if table  == self.searchDisplayController!.searchResultsTableView {

       var  dict = searchResults?.objectAtIndex(indexpath.row) as NSDictionary
        cell.textLabel?.text = ( dict.objectForKey("NAME") as String)

    }
    else{

        var dict = tasksList?.objectAtIndex(indexpath.row) as NSDictionary
        cell.textLabel?.text = ( dict.objectForKey("NAME") as String)
    }



    if(indexpath.row % 2 == 0){
        cell.backgroundColor = UIColor.clearColor()
    }else{
        //set cell opacity
        cell.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.2)
    }


    return cell
}

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

    var dict=tasksList?.objectAtIndex(indexPath.row) as NSDictionary

    //create an instance of detailVC
    var detail:detailVC=self.storyboard?.instantiateViewControllerWithIdentifier("detailVC")as detailVC

    //reference DetailVc s var cellName and assign it to the DetailVC s var items
    detail.cellName = dict.objectForKey("NAME") as String
    detail.cellDesc = dict.objectForKey("DESC") as String

    //programatically push to associated vc "DetailVC"
    self.navigationController?.pushViewController(detail, animated: true)
}

}

0 个答案:

没有答案