简体   繁体   中英

Swift - Expand tableview without Storyboard segue

I am trying to create a similar tableview like the one below:

https://github.com/justinmfischer/SwiftyExpandingCells

I already have a complete tableview with contents, including a filtering system. The current problem is that I want to implement the same functionality to expand cells and show detailed information, without using storyboard .

The sample code (link) uses a storyboard segue, but I want to get rid of that so that it can basically do the same thing through code. I removed the label for the background. Just want to get the title and a new controller first by clicking a tableview cell.

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
    }
}
}

This part needs to change

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
    }
}
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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM