我想選擇一行然後將其索引到另一個視圖控制器。我已經創建了一個「didselectrow」的函數,但是這不會導航到下一個視圖控制器。任何解決方案或幫助將不勝感激。帶有tableview的ViewController到另一個視圖控制器
進口的UIKit 進口CoreData
class CoreViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var users : [Users] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
// Do any additional setup after loading the view.
}
//view the data on the table view appear
override func viewDidAppear(_ animated: Bool) {
// get data
getData()
//reload data
tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let user = users[indexPath.row]
if user.isimportant
{
cell.textLabel?.text = "\(user.username!)"
}
else
{
cell.textLabel?.text = user.username! //entity name in textlabel
}
return cell
}
func getData()
{
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do{
users = try context.fetch(Users.fetchRequest())
}
catch {
print ("Fetching Failed ")
}
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
if editingStyle == .delete
{
let user = users[indexPath.row]
context.delete(user)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
do{
users = try context.fetch(Users.fetchRequest())
}
catch {
print ("Fetching Failed ")
}
}
tableView.reloadData()
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: QuestionsViewController) {
performSegue(withIdentifier: "questionview", sender: nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
哪裏是你的preapareforSegue方法寫在你的代碼 –
你確定你是在故事板上製作的? –
執行segue是在函數didselectrow – Jonathon