Patrick Lorran
Patrick Lorran

Reputation: 21

How to link Table View dynamic cells to specific View Controllers?

I'm a beginner developer in iOS 9 and Swift 2. I have a Table View with dynamic and grouped content and I want to link each cell with a specific View Controller (not a detail controller). I know how to do this using static cells with segues, but how can I do with dynamic cells? Fist, I'm using dynamic cells 'cause I plan to add much data, and second 'cause I implemented a search control to quickly find the desired entry. Plus, this specific link between the views must work when the user pick the cell from the search results. Here is my code:

import UIKit

class TableViewController: UITableViewController, UISearchResultsUpdating {

// MARK: Properties

let section = ["Section 1", "Section"]
let names = [["Name 1", "Name 2", "Name 3"], ["Name 4", "Name 5", "Name 6", "Name 7", "Name 8"]]
var tableData = ["Name 1", "Name 2", "Name 3", "Name 4", "Name 5", "Name 6", "Name 7", "Name 8"] // Look for a way to convert directly from "names" array!
var filteredData:[String] = []
var resultSearchController:UISearchController!

override func viewDidLoad() {
    super.viewDidLoad()
    resultSearchController = UISearchController(searchResultsController: nil)
    resultSearchController.searchResultsUpdater = self
    resultSearchController.hidesNavigationBarDuringPresentation = false
    resultSearchController.dimsBackgroundDuringPresentation = false
    resultSearchController.searchBar.searchBarStyle = UISearchBarStyle.Prominent
    resultSearchController.searchBar.sizeToFit()
    self.tableView.contentOffset = CGPointMake(0,CGRectGetHeight(resultSearchController.searchBar.frame))
    self.tableView.tableHeaderView = resultSearchController.searchBar
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if resultSearchController.active {
        return nil
    } else {
        return self.section[section]
    }
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    if resultSearchController.active {
        return 1
    } else {
        return self.section.count
    }
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    if resultSearchController.active {
        return filteredData.count
    } else {
        return self.names[section].count
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath)

    // Configure the cell...
    if resultSearchController.active {
        cell.textLabel?.text = filteredData[indexPath.row]
    } else {
        cell.textLabel?.text = self.names[indexPath.section][indexPath.row]
    }
    return cell
}

func updateSearchResultsForSearchController(searchController: UISearchController) {
    if searchController.searchBar.text?.characters.count > 0 {
        filteredData.removeAll(keepCapacity: false)
        let searchPredicate = NSPredicate(format: "SELF CONTAINS %@", searchController.searchBar.text!)
        let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
        filteredData = array as! [String]
        tableView.reloadData()
    }
    else {
        filteredData.removeAll(keepCapacity: false)
        filteredData = tableData
        tableView.reloadData()
    }
}

I didn't found a solution anywhere. As I haven't much experience, please write details and some code. Thanks!

EDIT: Being more specific:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

// Here is the code I need to link the selected cell to the
// right View Controller in my storyboard. And this must work
// when the user pick the cell in search results view too.
// For example, if the cell contains "Name 1", it navigates
// to "View Controller 1".

}

Upvotes: 1

Views: 2938

Answers (3)

Amir
Amir

Reputation: 13

Click on the cell that you want to link and then control drag to the view you want to appear. Then choose your option Ex: Show, Present Modally etc

Upvotes: 0

Patrick Lorran
Patrick Lorran

Reputation: 21

Thanks for the hints! Here's what I wrote:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {        let rowIndexPath = tableView.indexPathForSelectedRow!
    var nameString:String
    var segueString:String
    if resultSearchController.active {
        resultSearchController.active = false
        nameString = filteredData[rowIndexPath.row].lowercaseString
        segueString = "\(nameString)" + "View"
        self.performSegueWithIdentifier(segueString, sender: self)
    } else {
        nameString = self.names[rowIndexPath.section][rowIndexPath.row].lowercaseString
        segueString = "\(nameString)" + "View"
        self.performSegueWithIdentifier(segueString, sender: self)
    }
}

I created segues named "nameXView". So, for example, the segue linking "Name 1" and "View Controller 1" is "name1View". Then, I can pick the right segue according the cell content. And it works with search results too!

Upvotes: 0

Michael
Michael

Reputation: 9042

You can use the following construct to run your own code when a cell is touched.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // Your code here
}

Your code might performSegue, or anything else you like.

Upvotes: 1

Related Questions