我的代码很简单:
var searchResults1: [(name:String, description:String)]=[]
var searchResults2: [(name:String, description:String)]=[]
var searchResults3: [(name:String, description:String)]=[]
var situation1 = [(name: "qq", description: "ww"), (name: "ee", description: "rr"), (name: "tt", description: "yy"), (name: "aa", description: "ss")]
var situation2 = [(name: "qq", description: "ff"), (name: "gg", description: "hh"), (name: "jj", description: "kk"), (name: "ll", description: "ii")]
var situation3 = [(name: "zz", description: "xx"), (name: "cc", description: "vv"), (name: "bb", description: "nn"), (name: "mm", description: "as"), (name: "we", description: "sd"), (name: "xc", description: "gf")]
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
switch section {
case 0:
return "I"
case 1:
return "II"
case 2:
return "III"
case 3:
return "IV"
case 4:
return "V"
case 5:
return "VI"
case 6:
return "VII"
case 7:
return "VIII"
case 8:
return "IX"
case 9:
return "X"
case 10:
return "XI"
case 11:
return "XII"
default:
return nil
}
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 12
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "cell"
var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: identifier)
}
if (indexPath.section == 0) {
if tableView == self.searchDisplayController!.searchResultsTableView {
cell!.textLabel.text = searchResults1[indexPath.row].name
} else {
cell!.textLabel.text = situation1[indexPath.row].name
}
return cell!
}
if (indexPath.section == 1) {
if tableView == self.searchDisplayController!.searchResultsTableView {
cell!.textLabel.text = searchResults2[indexPath.row].name
} else {
cell!.textLabel.text = situation2[indexPath.row].name
}
return cell!
}
if (indexPath.section == 2) {
if tableView == self.searchDisplayController!.searchResultsTableView {
cell!.textLabel.text = searchResults3[indexPath.row].name
} else {
cell!.textLabel.text = situation3[indexPath.row].name
}
return cell!
}
return cell!
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.searchResults1.count
} else {
return situation1.count }
case 1:
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.searchResults2.count
} else {
return situation2.count }
case 2:
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.searchResults3.count
} else {
return situation3.count }
default:
return 0
}
}
func filterContentForSearchText (searcText: String) {
searchResults1 = situation1.filter{($0.name as NSString).localizedCaseInsensitiveContainsString("\(searcText)")}
searchResults2 = situation2.filter{($0.name as NSString).localizedCaseInsensitiveContainsString("\(searcText)")}
searchResults3 = situation3.filter{($0.name as NSString).localizedCaseInsensitiveContainsString("\(searcText)")}
}
func searchDisplayController(controller:UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool {
self.filterContentForSearchText(searchString)
return true
}
override func tableView(tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath){
if tableView == self.searchDisplayController!.searchResultsTableView {
self.performSegueWithIdentifier("showDetail", sender: self)
}
}
这很好。
当我点击单元格searchResultsTableView错误输出时:线程1:EXC_BREAKPOINT(代码= EXC_I386_BPT,子代码= 0x0)
示例代码函数prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender:AnyObject!) {
if segue.identifier == "showDetail" {
let cell = sender as UITableViewCell
var indexPath = tableView.indexPathForCell(cell)!
switch (indexPath.section) {
case 0:
if self.searchDisplayController!.active {
var indexPath = tableView.indexPathForSelectedRow()
indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()
var destViewController: DetailViewController = segue.destinationViewController as DetailViewController
destViewController.title = searchResults1[indexPath!.row].name
destViewController.situation = searchResults1[indexPath!.row]
}
else {
var indexPath = tableView.indexPathForSelectedRow()
let destViewController:DetailViewController! = segue.destinationViewController as DetailViewController
destViewController.title = situation1[indexPath!.row].name
destViewController.situation = situation1[indexPath!.row]
}
case 1:
if self.searchDisplayController!.active {
var indexPath = tableView.indexPathForSelectedRow()
indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()!
var destViewController: DetailViewController = segue.destinationViewController as DetailViewController
destViewController.title = searchResults2[indexPath!.row].name
destViewController.situation = searchResults2[indexPath!.row]
}
else {
var indexPath = tableView.indexPathForSelectedRow()
let destViewController:DetailViewController! = segue.destinationViewController as DetailViewController
destViewController.title = situation2[indexPath!.row].name
destViewController.situation = situation2[indexPath!.row]
}
default:
break
}
}
你能否建议我如何修改prepareForSegue函数?
链接到DropBox https://www.dropbox.com/sh/3yeowjurweo3xdo/AAAh7S-N89XPSqi-Xr8M5WmPa?dl=0
中的项目谢谢!
答案 0 :(得分:0)
您的performSegueWithIdentifier
来电通过self
作为sender
参数:
self.performSegueWithIdentifier("showDetail", sender: self)
您的prepareForSegue
方法希望发件人成为UITableViewCell:
let cell = sender as UITableViewCell
所以我猜测崩溃就在那一行,其中强制转换会失败,因为你的UITableView派生类不能转换为UITableViewCell。
答案 1 :(得分:0)
试试这个:
1. self.performSegueWithIdentifier("showDetail", sender: cell!)
2. if segue.identifier == "showDetail" {
let cell = sender as UITableViewCell
var ip = tableView.indexPathForCell(cell);
if (ip == nil) {
ip = self.searchDisplayController!.searchResultsTableView.indexPathForCell(cell);
}
var indexPath = ip!
switch (indexPath.section) {
case 0: