我已经研究过如何在表视图中创建一个搜索栏,但是它们都使得tableview的数据不同,所以当我尝试在我的tableview上使用他们的方法时,标题,细节和图片都放错了位置。 如何从代码中创建搜索栏?
class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate {
var cellContent = ["not actual data for privacy reasons, there are 23 strings in here"]
var cellDetail = ["not actual data for privacy reasons, there are 23 strings in here"]
var cellImage = ["not actual data for privacy reasons, there are 23 names of images in here"]
let locationManager = CLLocationManager()
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 {
self.performSegueWithIdentifier("0", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 1 {
self.performSegueWithIdentifier("1", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 2 {
self.performSegueWithIdentifier("2", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 3 {
self.performSegueWithIdentifier("3", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 4 {
self.performSegueWithIdentifier("4", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 5 {
self.performSegueWithIdentifier("5", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 6 {
self.performSegueWithIdentifier("6", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 7 {
self.performSegueWithIdentifier("7", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 8 {
self.performSegueWithIdentifier("8", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 9 {
self.performSegueWithIdentifier("9", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 10 {
self.performSegueWithIdentifier("10", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 11 {
self.performSegueWithIdentifier("11", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 12 {
self.performSegueWithIdentifier("12", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 13 {
self.performSegueWithIdentifier("13", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 14 {
self.performSegueWithIdentifier("14", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 15 {
self.performSegueWithIdentifier("15", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 16 {
self.performSegueWithIdentifier("16", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 17 {
self.performSegueWithIdentifier("17", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 18 {
self.performSegueWithIdentifier("18", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 19 {
self.performSegueWithIdentifier("19", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 20 {
self.performSegueWithIdentifier("20", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 21 {
self.performSegueWithIdentifier("21", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
} else if indexPath.row == 22 {
self.performSegueWithIdentifier("22", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int {
return 23
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = (tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!)
if (cell == cell) {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
var imageName = UIImage(named: cellImage[indexPath.row])
//let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
//let cell2 = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
//let art = cellContent[indexPath.row]
cell.textLabel?.text = cellContent[indexPath.row]
cell.detailTextLabel?.text = self.cellDetail[indexPath.row]
cell.imageView?.image = imageName
//cell.backgroundColor = UIColor.clearColor()
//cell.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.1)
return cell
}
return cell
}
}
答案 0 :(得分:0)
在cellForRowAtIndexPath:
中,您必须检查TableView
是处于搜索模式还是常规模式。然后,您可以让TableView
从相应的数据源中取出相应的单元格,具体取决于表格所处的状态。最好使用if...else
答案 1 :(得分:0)
作为整理代码的旁注。而不是对didSelect上的每个indexPath执行if语句,只需将indexPath行转换为字符串并将其用作segue标识符即可编写它:
self.performSegueWithIdentifier(String(indexPath.row), sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)