我想选择一行,然后将其索引到另一个视图控制器。我创建了一个“didselectrow”的功能,但这并没有导航我到下一个视图控制器。任何解决方案或帮助将不胜感激。
导入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.
}
*/
}
答案 0 :(得分:1)
试试这个!
从
调用seguefunc 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]
}
}
答案 1 :(得分:1)
你需要调用didselectAtIndexPathRow
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegue(withIdentifier: "questionview", sender: nil)
}
答案 2 :(得分:0)
你的didSelectRow方法不正确你所做的是将QuestionViewController作为indexpath的类型传递
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: QuestionsViewController) {
performSegue(withIdentifier: "questionview", sender: nil)
}
应该是,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "questionview", sender: nil)
}
答案 3 :(得分:0)
你可以试试这个
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)
}
答案 4 :(得分:0)
查看下面给出的链接。这将帮助您从头开始构建表视图。
https://code.tutsplus.com/tutorials/ios-from-scratch-with-swift-table-view-basics--cms-25160
答案 5 :(得分:0)
步骤1:添加2个视图控制器
步骤2:在第一个viewController中添加表视图
步骤3:在表视图中添加tableview单元格(给出标识符" cell")
步骤4:检查表视图属性
步骤5:准备segue方式(第一视图控制器到第二视图控制器)
第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.
}
*/
}