简体   繁体   中英

Creating new views from selecting a tableview cell

Right now I have a tableview that has a dynamic cell with a basic style. Currently, the list of names shows in a tableview, but I want to use the didSelectRowAt function to create new detailed views for each row. How would I create a new view controller that is customizable for every time a certain row is selected from my tableview? Also, my storyboard is connected from the cell to another viewController named 'SecondViewController' through a segue called 'show'.

Here is the code I have so far.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableView: UITableView!

struct Data {
    var sectionTitle = String()
    var rowTitles = [String]()
}

var dataArray = [Data(sectionTitle: "section 1", rowTitles: ["row 1", "row 2"]),
    Data(sectionTitle: "section 2", rowTitles: ["row 1", "row 2", "row 3"]),
    Data(sectionTitle: "section 3", rowTitles: ["row 1"])
]
var searchArray = [Data]()
var searching = false

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    <#code#>
}

}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searching {
        return searchArray[section].rowTitles.count
    } else {
        return dataArray[section].rowTitles.count
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell")
    cell?.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
    cell?.textLabel?.numberOfLines = 3
    if searching {
        cell?.textLabel?.text = self.searchArray[indexPath.section].rowTitles[indexPath.row]
    } else {
        cell?.textLabel?.text = self.dataArray[indexPath.section].rowTitles[indexPath.row]
    }
    return cell!
}
func numberOfSections(in tableView: UITableView) -> Int {
    if searching {
        return searchArray.count
    } else {
        return dataArray.count
    }
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return dataArray[section].sectionTitle
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "show", sender: self)
}

}

extension ViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchBar.text == nil || searchBar.text == "" {
        searching = false
        view.endEditing(true)
        tableView.reloadData()
    } else {
        searching = true
        searchArray = dataArray.filter({$0.sectionTitle.lowercased().contains(searchBar.text!.lowercased()) || $0.rowTitles.map{$0.lowercased()}.contains(searchBar.text!.lowercased())})
        tableView.reloadData()
    }
}
}

First I'm not sure if your struct really is called Data in case it is you should change it to something with more detail.

Move your struct outside of that view controller so it's global.

Create a new View controller

import UIKit

class DetailedController: UIViewController {

    var data: Data!

    init(data: Data) {
        self.data = data
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}

Within your didSelectRowAtMethod

let index = indexPath.row
let data = dataArray[index]
let detailedVC = DetailedController(data: data)
navigationController?.pushViewController(detailedVC, animated: true)

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