Swift - 在没有Storyboard segue的情况下展开tableview

时间:2016-08-29 05:42:07

标签: ios swift

我正在尝试创建类似下面的表格视图:

https://github.com/justinmfischer/SwiftyExpandingCells

我已经拥有包含过滤系统内容的完整tableview。目前的问题是我想实现相同的功能来扩展单元格并显示详细信息,不使用故事板

示例代码(链接)使用了storyboard segue,但我想摆脱它,以便它基本上可以通过代码执行相同的操作。我删除了背景标签。只想通过单击tableview单元格获得标题和新控制器。

DetailViewController

class DetailVC: UIViewController {

var brand: Brand?

override func viewDidLoad() {
    super.viewDidLoad()

    self.setup()
}

func setup() {
    self.view.frame.origin.y = UIApplication.sharedApplication().statusBarFrame.size.height

    if let brand = self.brand {
        self.title = brand.name
    }
}
}

此部分需要更改

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.selectedCellFrame = tableView.convertRect(tableView.cellForRowAtIndexPath(indexPath)!.frame, toView: tableView.superview)
    self.selectedBrand = BrandManager.sharedInstance.brands[indexPath.row]

    self.performSegueWithIdentifier(.DetailVC , sender: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    switch segueIdentifierForSegue(segue) {
        case .DetailVC:
            let vc = segue.destinationViewController as! DetailVC
                vc.brand = self.selectedBrand

            self.navigationController?.delegate = self
    }
}

1 个答案:

答案 0 :(得分:0)

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.selectedCellFrame = tableView.convertRect(tableView.cellForRowAtIndexPath(indexPath)!.frame, toView: tableView.superview)
        self.selectedBrand =  BrandManager.sharedInstance.brands[indexPath.row]

        let vc = storyboard?.instantiateViewControllerWithIdentifier("DetailVc") as! DetailVC
        vc.brand = self.selectedBrand
        self.navigationController?.pushViewController(vc, animated: true)
     }//// add the storyboardID for the detailsVc as "DetailVc"