我无法用我的桌子定义一个单元格。它允许用户搜索城市,这是给我带来麻烦的代码。这是视图控制器的完整代码。目标是允许用户在列表中搜索城市。
import UIKit
import Foundation
import CoreData
class SearchBar: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate{
var searchActive : Bool = false
var data: [String] = ["San Francisco","New York","San Jose","Chicago","Los Angeles","Austin","Seattle"]
var filtered: [String] = []
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var checkavailability: UIBarButtonItem!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var addselected: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
/* Setup delegates */
tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self
}
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 = data.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.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(searchActive) {
return filtered.count
}
else {
return data.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell!
if(searchActive == true){
cell.textLabel?.text = filtered[indexPath.row]
}
else {
cell.textLabel?.text = data[indexPath.row] \\ this is where the error comes up
}
return cell
}
}
答案 0 :(得分:1)
尝试使用tableView而不是TableView,即使用小写' t。小写tableView是一个类实例,大写的TableView是一个类。