[英]ViewController with a tableview to another view controller
我想選擇一行,然后將其索引到另一個視圖控制器。 我創建了一個函數“ 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.
}
*/
}
嘗試這個!
呼叫來自
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]
}
}
您需要調用didselectAtIndexPathRow
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegue(withIdentifier: "questionview", sender: nil)
}
您的didSelectRow方法不正確,將您所做的事情作為索引路徑的類型傳遞給QuestionsViewController
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: QuestionsViewController) {
performSegue(withIdentifier: "questionview", sender: nil)
}
它應該是,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "questionview", sender: nil)
}
你可以試試看
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)
}
查看下面給出的鏈接。 這將幫助您從頭開始構建表格視圖。
https://code.tutsplus.com/tutorials/ios-from-scratch-with-swift-table-view-basics--cms-25160
步驟1:添加2個View Controller
步驟2:在第一個viewController中添加表視圖
第3步:在表格視圖中添加表格視圖單元格(給標識符“單元格”)
步驟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.
}
*/
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.