您好
如图所示,当我搜索任何内容时,它会在原始单元格上覆盖结果。白色的结果没有任何搜索,黑色的是搜索的结果。我对clickin单元格和segue没有任何问题或错误。什么可能导致这个?谢谢!
PS:我的tableview通过互联网从3个不同的MS SQL查询填充,它们合并在一个数组中。因此,当我搜索它时,不会重新查询MS SQL
这是我的代码:“我删除了一些不必要的部分,比如效果等等”
import UIKit
class KalipTableViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {
var link = ""
var profillistesi = [String]()
@IBOutlet weak var searchBar: UISearchBar!
var searchActive : Bool = false
var filtered:[String] = []
@IBAction func getir(sender: AnyObject) {
if self.link == "checking external ip here" {
var client = SQLClient()
client.connect("server-ip", username: "username", password: "password", database: "database-name") {
success in
if success {
client.execute("select KalipNo from Kaliplar order by KalipNo asc") {
sonuc in
for table in sonuc as NSArray {
for row in table as! NSArray {
for column in row as! NSDictionary {
self.profillistesi.append("\(column.value)")
}
}
}
client.disconnect()
}
}
}
} else if self.link == "checking emergency external ip here"
var client = SQLClient()
client.connect("server-ip", username: "username", password: "password", database: "database-name") {
success in
if success {
client.execute("select KalipNo from Kaliplar order by KalipNo asc") {
sonuc in
for table in sonuc as NSArray {
for row in table as! NSArray {
for column in row as! NSDictionary {
self.profillistesi.append("\(column.value)")
}
}
}
client.disconnect()
}
}
}
} else {
var client = SQLClient()
client.connect("server-ip", username: "username", password: "password", database: "database-name") {
success in
if success {
client.execute("select KalipNo from Kaliplar order by KalipNo asc") {
sonuc in
for table in sonuc as NSArray {
for row in table as! NSArray {
for column in row as! NSDictionary {
self.profillistesi.append("\(column.value)")
}
}
}
client.disconnect()
self.tableView.reloadData()
}
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self
let url = NSURL(string: "external ip of server"
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
//print(NSString(data: data!, encoding: NSUTF8StringEncoding))
self.link = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(searchActive) {
return filtered.count
}
return profillistesi.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> KalipTableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! KalipTableViewCell
if(searchActive){
cell.textLabel?.text = filtered[indexPath.row]
} else {
cell.kalipCell.text = profillistesi[indexPath.row] as? String
}
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "kalipSegue" {
let gecis: KalipViewController = segue.destinationViewController as! KalipViewController
let gecis2 = tableView.indexPathForSelectedRow?.row
gecis.kalipNo = profillistesi[gecis2!]
}
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false;
self.searchBar.endEditing(true)
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false;
self.searchBar.endEditing(true)
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false;
self.searchBar.endEditing(true)
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
filtered = profillistesi.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()
}
}