prepareForSegue 错误线程 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
Posted
技术标签:
【中文标题】prepareForSegue 错误线程 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)【英文标题】:prepareForSegue an error Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0) 【发布时间】:2014-12-02 18:22:50 【问题描述】:我有简单的代码:
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 (code=EXC_I386_BPT, subcode=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
谢谢!
【问题讨论】:
当您抛出异常时,提供完整的堆栈跟踪(显示在调试导航器中)并指出发生异常的确切行非常有帮助。 【参考方案1】:试试这个:
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:
【讨论】:
【参考方案2】:您的performSegueWithIdentifier
调用将self
作为sender
参数传递:
self.performSegueWithIdentifier("showDetail", sender: self)
您的 prepareForSegue
方法要求发送者是 UITableViewCell:
let cell = sender as UITableViewCell
所以我猜想崩溃就在那条线上,转换将失败,因为您的 UITableView 派生类将无法转换为 UITableViewCell。
【讨论】:
感谢您的回答。我应该怎么做才能在搜索结果和 DetailViewController 之间架起一座桥梁? 好吧,您可能应该使用cellForRowAtIndexPath
查找您需要的单元格并将其作为发件人传递给performSegueWithIdentifier
,或者只使用prepareForSegue
中的indexPathForSelectedRow
来查找直接选择行,而不是使用 sender
... 取决于你,真的。
谢谢!收到如下函数: if self.searchDisplayController!.active var indexPath = tableView.indexPathForSelectedRow() indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow() var destViewController: DetailViewController = segue.destinationViewController as DetailViewController destViewController.situation = searchResults1[indexPath !.row]
当我单击第 1 节的搜索结果单元格时,一切正常。当我单击第 2 节的搜索结果单元格时,出现错误:致命错误:数组索引超出排名。我知道这与:destViewController.situation = searchResults1[indexPath!.row] 有关。我无法将 DetailViewController 转换为 searchResults2、searchResults3。有什么建议?以上是关于prepareForSegue 错误线程 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)的主要内容,如果未能解决你的问题,请参考以下文章
prepareForSegue 错误线程 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
Swift PrepareForSegue EXC_BREAKPOINT
将视图控制器分配给 prepareForSegue 中的变量时的 Swift EXC_BREAKPOINT