为单个UITableView页面设置UISearchBar

时间:2016-04-20 15:12:23

标签: ios swift uitableview

我尝试使用Swift 2创建单页Table View应用程序。

我一直试图通过UISearchBar实现搜索功能,但是,我找到的每个教程都使用Master-Detail应用程序或Objective-C。

首先,这是我的代码:

import UIKit

class TableViewController: UITableViewController {

struct Objects {
    var sectionName : String!
    var sectionObjects : [String!]
}

var objectsArray = [Objects]()


override func viewDidLoad() {
    super.viewDidLoad()

    objectsArray = [Objects(sectionName: "test", sectionObjects: ["1", "2", "3"])]   
}

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

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

    cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]

    return cell
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return objectsArray[section].sectionObjects.count

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return objectsArray.count

}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return objectsArray[section].sectionName

    }
}

我也不需要过滤。我只想输入搜索栏并查看相应的结果(例如搜索" 1"," 1"结果出现)。

我只有一个ViewController Swift文件,没有MasterView Swift文件,因此教程可能无法正常工作。

0 个答案:

没有答案