2017-03-11 60 views
0

我具有以下設置的tableView:Segueing與許多段

enter image description here

我想執行從每個細胞的SEGUE(由公開的內容所指示的)到另一個視圖控制器。問題是我不想製作六個不同的視圖控制器,特別是因爲它們中的許多將幾乎相同,包含一個或兩個文本字段和一個標籤。有什麼辦法可以讓一個視圖控制器,並改變它被點擊單元格?

+0

什麼是從創建一個視圖控制器和配置在'prepareForSegue'該目標視圖控制器阻止你? –

+0

在你的'prepareForSegue'方法,調用'tableVIew.indexPathForSelectedRow'知道被竊聽什麼細胞。然後相應地配置你的目標視圖控制器 –

回答

0
Create a new View Controller i am naming it as "VController" 

Create enum in "VController" above the view didload : 

    public enum VCType:Int { 
     case name = 1 
     case school = 2 
     case grade = 3 
    } 
    var selectedVc:VCType! 

In viewdidload of "VController": 

if selectedVc == .name { 
      // do whatever you want 
     } 
     else if selectedVc == .school { 
      // do whatever you want 
     } 
     else if 
     .......... 
     .......... 


Code For First ViewController : 

above the view didload : 

var selectedType:VController.VCType! 


    // in didSelectRowAtIndexPath 
    selectedType = .school // if selecting school 
    perform segue 


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
      if segue.identifier == "mySegue"{ 
       let nextScene = segue.destination as? VController 
       nextScene?.selectedVc = selectedType 
      } 
     } 
0

的問題是,我不想做六個不同的視圖控制器, 特別是因爲許多人將幾乎相同,包含一個 或兩個文本字段和標籤。

這絕對是真實的。您可以通過添加只有一個賽格瑞 -without需要從設置的ViewController到下一個(詳細信息視圖控制器)將六個不同segues-的實現這一點;根據哪一行 - 在哪一部分 - 已被選中,您可以執行您的搜尋併發送所需的數據。

1添加Segue公司:

你需要從設置視圖控制器中添加SEGUE的細節視圖控制器。 從任何表觀察室確保該SEGUE已經從視圖控制器本身的設置連接,但不。在storyboard上添加segue之後,您需要爲其添加標識符,我會將其稱爲 - 在我的代碼片段示例中 - 「toDetails」。

如果您不知道如何爲segue添加標識符,則可能需要檢查this answer

2-發送所需要的數據:

爲了簡化的目的,我認爲你要發送的數據只是一個字符串變量,叫我-in代碼片段例 - dataToSend

它去如下:

設置視圖控制器:

class SettingsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
@IBOutlet weak var tableView: UITableView! 

//... 
private var dataToSend = "" 
//... 

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

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

    // tableView should be connected as an IBOutlet 
    guard let selectedIndexPath = tableView.indexPathForSelectedRow else { 
     print("Something went wrong when selected a row!") 
     return 
    } 

    if selectedIndexPath.section == 0 { // Account Section 

     if selectedIndexPath.row == 0 { // Name Row 
      dataToSend = "name" 
     } else if selectedIndexPath.row == 1 { // School Row 
      dataToSend = "school" 
     } else if selectedIndexPath.row == 2 { // Grade Row 
      dataToSend = "grade" 
     } 

    } else if selectedIndexPath.section == 1 { // Private Section 

     if selectedIndexPath.row == 0 { // Email Row 
      dataToSend = "email" 
     } else if selectedIndexPath.row == 1 { // Password Row 
      dataToSend = "password" 
     } else if selectedIndexPath.row == 2 { // Phone Number Row 
      dataToSend = "phone" 
     } 

    } 

    let detailsViewController = segue.destination as! DetailsViewController 
    detailsViewController.receivedData = dataToSend 
    } 

    //... 
} 

採取進一步:

這樣的情況下工作,使用枚舉時,這是一個很好的做法而不是檢查行號,這導致更易讀的代碼:

enum Sections:Int { 
    case 
    account = 0, 
    privacy = 1 
} 

enum AccountRows:Int { 
    case 
    name = 0, 
    school = 1, 
    grade = 2 
} 

enum PrivacyRows:Int { 
    case 
    email = 0, 
    password = 1, 
    phone = 2 
} 

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
    //... 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

     // tableView should be connected as an IBOutlet 
     guard let selectedIndexPath = tableView.indexPathForSelectedRow else { 
      print("Something went wrong when selected a row!") 
      return 
     } 

     // tuple of (section, row) 
     switch (selectedIndexPath.section, selectedIndexPath.row) { 
      // Accounts Section 
      case (Sections.account.rawValue, AccountRows.name.rawValue): 
       dataToSend = "name" 
      case (Sections.account.rawValue, AccountRows.school.rawValue): 
       dataToSend = "school" 
      case (Sections.account.rawValue, AccountRows.grade.rawValue): 
       dataToSend = "grade" 

      // Privacy Section 
      case (Sections.privacy.rawValue, PrivacyRows.email.rawValue): 
       dataToSend = "email" 
      case (Sections.privacy.rawValue, PrivacyRows.password.rawValue): 
       dataToSend = "password" 
      case (Sections.privacy.rawValue, PrivacyRows.phone.rawValue): 
       dataToSend = "phone" 
      default: 
       print("Something went wrong when checking Section and Rows!") 
     } 

     let detailsViewController = segue.destination as! DetailsViewController 
     detailsViewController.receivedData = dataToSend 
    } 
    //... 
} 

歡呼起來!希望這有助於。