简体   繁体   English

具有到另一个视图控制器的tableview的ViewController

[英]ViewController with a tableview to another view controller

I want to select a row and then index this to another view controller. 我想选择一行,然后将其索引到另一个视图控制器。 I have created a function which is "didselectrow" however this is not navigating me to the next view controller. 我创建了一个函数“ didselectrow”,但这并不能使我导航到下一个视图控制器。 Any solutions or help would be much appreciated. 任何解决方案或帮助将不胜感激。

import UIKit import CoreData 导入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.
}
*/

}

Try this! 尝试这个!

Call the segue from 呼叫来自

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

        self.performSegueWithIdentifier("questionview", sender: nil)

    }

 override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "questionview") {
        let viewController:ViewController = segue!.destinationViewController as ViewController
        let indexPath = self.tableView.indexPathForSelectedRow()
        viewController.mydata = self.myarray[indexPath.row]
    }
}

you need to call in didselectAtIndexPathRow 您需要调用didselectAtIndexPathRow

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

    performSegue(withIdentifier: "questionview", sender: nil)
}

Your didSelectRow method is not correct what you have done is passed QuestionsViewController as type for indexpath 您的didSelectRow方法不正确,将您所做的事情作为索引路径的类型传递给QuestionsViewController

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: QuestionsViewController) {
    performSegue(withIdentifier: "questionview", sender: nil)
}

It should be, 它应该是,

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "questionview", sender: nil)
    }

You can Try this 你可以试试看

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{

    let cell = self.tableView.cellForRow(at: indexPath as IndexPath)

self.performSegue(withIdentifier: "yoursegueIdentifier", sender: nil)

} }

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{

    let theDestinationVC = (segue.destination as! NextVC)

}

Check out the link given below. 查看下面给出的链接。 This will help you building a table view from scratch. 这将帮助您从头开始构建表格视图。

https://code.tutsplus.com/tutorials/ios-from-scratch-with-swift-table-view-basics--cms-25160 https://code.tutsplus.com/tutorials/ios-from-scratch-with-swift-table-view-basics--cms-25160

step 1 : Add 2 View Controller 步骤1:添加2个View Controller

Step 2 : Add Table View in 1st viewController 步骤2:在第一个viewController中添加表视图

Step 3 : add tableview cell in Table view (give identifier "cell") 第3步:在表格视图中添加表格视图单元格(给标识符“单元格”)

Step 4 : Check table view Property 步骤4:检查表视图属性

步骤4图片

Step 5 : Prepare segue way (1st view controller to 2nd View Controller) 步骤5:准备segue方式(第一视图控制器到第二视图控制器)

如何准备这首歌

Step 6 : here is full code 步骤6:这是完整的代码

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



    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "questionview" {

        }
    }
    /*
     // 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.
     }
     */

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解雇一个视图控制器并提出另一个viewController - dismissing a view controller and presenting another viewController Tableview 到另一个视图控制器 - Tableview to another viewcontroller 一个视图控制器中的多个tableview控制器 - multiple tableview controller in one viewcontroller 以编程方式将视图控制器添加到导航控制器,然后推送另一个视图控制器 - Add viewcontroller to navigation controller programmatically then push another view controller 使用TableView将UIImageView加载到另一个ViewController - Load UIImageView with TableView to another ViewController NavigationController正在显示另一个视图控制器,而不是推送ViewController - NavigationController is showing another view controller instead of pushing viewcontroller 根据另一个视图控制器更改viewController BG颜色 - Change viewController BG color based off another view controller 将UISwitch状态从一个View Controller传递到另一个ViewController - Pass UISwitch State from One View Controller to Another ViewController 在加载另一个ViewController时呈现模态视图控制器 - Presenting a modal view controller when loading another ViewController viewcontroller-如何获取另一个根视图控制器/如何切换视图 - viewcontroller - how to get another root view controller / how to switch Views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM