uitableview - prepareForSegue 错误 Thread 1 : EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)

标签 uitableview swift

我有简单的代码:

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 错误输出:Thread 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.  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:

关于uitableview - prepareForSegue 错误 Thread 1 : EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27256341/

相关文章:

ios - Swift 如何将文档文件 URL 转换为字符串,以便在 tableview 中显示文件名

ios - 使用NSAutoresizingMaskLayoutConstraint进行UITableViewCell舍入错误,但是在Storyboard和heightForRowAtIndexPath中正确设置了大小:

ios - UITableViewController 在模拟器中显示白色空白屏幕

swift - 如何在 uiwebview (swift) 中调用 https url?

Swift字典错误访问

objective-c - 有没有快速的方法知道 UITableView 中设备屏幕外的单元格是什么?

ios - 关闭我添加的 View 后 UITableView 单元格消失

ios - UIAlertView 错误

swift - UIGestureRecognizer 导致未捕获的异常错误

ios - Swift:CGPathRelease 和 ARC