2015-11-20 73 views
0

我真的不知道要搜索什麼關於這個問題,我的搜索嘗試沒有太多的幫助,所以我想我會給這個帖子一個鏡頭。SWIFT UITableView - 在原型單元格中的多行繼續到另一個UITableView

我有一個將有三個行的原型單元格。我想要發生的事情是從每行顯示另一個UITableView的segue - 但我希望保持視圖控制器的數量最小,所以我希望這可以實現只有一個tableViewController。

例如,第1行是「外徑(OD)」,第2行是「重量」,第3行是「內徑(ID)」。這些被定義爲TableViewController文件的類聲明下的常量。

我有一個從上述TableViewController的segue(Modally)到第二個TableViewController,爲此我爲它創建了一個新文件。我在我的類聲明下定義了這個文件中的OD,Weight和ID值作爲常量。

有沒有什麼辦法可以達到這個目的,當用戶點擊OD行時,它只會顯示我在第二個視圖控制器中爲OD定義的值/常量,以及僅顯示何時的Weight和ID常量用戶分別點擊這些行?我的困惑是如何鏈接兩個TableViewControllers,以便它們可以互相交互...但我不確定這是否是所有可能的。我的代碼迄今如下:

表視圖控制器1:

import UIKit 

class CapacitiesTableViewController: UITableViewController { 

let capacityParameters = ["O.D", "Weight", "I.D"] 

@IBAction func cancelSelection(segue: UIStoryboardSegue) { 

} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    self.tableView.rowHeight = UITableViewAutomaticDimension 
    self.tableView.estimatedRowHeight = 125.0 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 2 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if section == 0 { 
     return capacityParameters.count 
    } else { 
     return 1 
    } 

} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    // init return type of cell 
    var returnCell: UITableViewCell! 

    if indexPath.section == 0 { // you can also check for section with section.index 
     returnCell = tableView.dequeueReusableCellWithIdentifier("capacitiesCell", forIndexPath: indexPath) 

     let capacityID = capacityParameters[indexPath.row] 
     returnCell.textLabel?.text = capacityID 

    return returnCell 

    } else { 

     returnCell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) 

     return returnCell 

    } 
} 


override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    if section == 0 { 
     return "Casing Specification" 
    } else { 
     return "Dimension & Strength" 
    } 
} 

視圖控制器2:

import UIKit 

class CasingSelectionTableViewController: UITableViewController { 

let casingOD = ["114.3", "127.0", "139.7", "168.3" , "177.8", "193.7", "219.1", "244.5", "247.6", "273.1", "298.4", "298.4", "339.7", "406.4", "473.0", "508"] 

let casingWeight114 = ["14.14", "15.63", "17.26", "20.09", "22.47", "24.70", "26.34", "27.97"] 
let casingWeight127 = ["17.11", "19.35", "22.32", "26.79", "31.85", "34.53", "35.86"] 
let casingWeight139 = ["20.83", "23.07", "25.30", "29.76", "34.23", "39.28", "44.20", "48.51", "52.53", "56.55", "60.27", "64.14"] 
let casingWeight168 = ["19.34", "25.30", "29.76", "32.74", "35.71", "38.69", "41.66", "43.15", "47.62", "50.59"] 
let casingWeight177 = ["25.30", "29.76", "34.23", "38.69", "43.16", "47.62", "52.09", "56.55"] 
let casingWeight193 = ["35.72", "39.29", "44.20", "50.15", "58.04"] 
let casingWeight219 = ["35.72", "41.67", "47.62", "53.57", "59.53"] 
let casingWeight244 = ["48.07", "53.57", "59.53", "64.73", "69.94", "79.62"] 
let casingWeight247 = ["88.09"] 
let casingWeight273 = ["48.74", "60.27", "67.71", "75.90", "82.59"] 
let casingWeight298 = ["56.54", "62.50", "69.94", "80.35", "89.28", "96.72", "105.65"] 
let casingWeight339 = ["71.43", "81.10", "90.78", "101.19", "107.15"] 
let casingWeight406 = ["96.73", "111.61", "125.01", "162.21"] 
let casingWeight473 = ["108.76", "116.06", "130.00", "143.59"] 
let casingWeight508 = ["139.89", "158.49", "197.93"] 

let id114 = ["103.886", "102.921", "101.600", "99.568", "97.180", "95.300", "93.900", "92.400"] 
let id127 = ["115.824", "114.148", "111.963", "108.610", "104.800", "104.800", "102.718", "101.600"] 
let id139 = ["127.305", "125.730", "124.257", "121.361", "118.618", "114.300", "111.150", "107.950", "104.800", "101.600", "98.450", "95.520"] 
let id168 = ["158.800", "155.800", "153.600", "152.100", "150.300", "148.700", "147.000", "146.300", "144.100", "142.100"] 
let id177 = ["166.065", "163.982", "161.696", "159.410", "157.074", "154.788", "152.502", "150.368"] 
let id193 = ["178.400", "177.000", "174.700", "171.900", "168.300"] 
let id219 = ["205.664", "203.632", "201.193", "198.700", "197.400"] 
let id244 = ["228.630", "226.590", "224.410", "222.380", "220.500", "216.790"] 
let id247 = ["217.400"] 
let id273 = ["258.877", "255.270", "252.730", "250.190", "247.904"] 
let id298 = ["283.200", "281.500", "279.400", "276.300", "273.600", "271.300", "268.800"] 
let id339 = ["322.961", "320.421", "317.881", "315.341", "313.614"] 
let id406 = ["387.350", "384.150", "381.254", "373.075"] 
let id473 = ["454.000", "453.500", "450.900", "448.400"] 
let id508 = ["485.750", "482.600", "475.742"] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return 0 
} 

/* 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 

    // Configure the cell... 

    return cell 
} 
*/ 

回答

1

首先去ViewController2.swift並聲明如下

enum Data 
{ 
    case OuterDiameter 
    case Weight 
    case Innerdiameter 
} 

class ViewController2: UIViewController, UITableViewDataSource, UITableViewDelegate { // Or sub class of UITableViewController 

    var selectedData:Data! 

    override func viewDidLoad() { 

    super.viewDidLoad() 
    switch self.selectedData { 
     case .OuterDiameter : 
      //arrData = fill outer data 
      break 
     case .Weight : 
      //arrData = fill weight 
      break 
     case .Innerdiameter : 
      //arrData = fill inner data 
      break 
    } 
    tblview.reloadData() 
    } 
} 

現在ViewController1.swift

class ViewController1: UIViewController, UITableViewDataSource, UITableViewDelegate { // Or sub class of UITableViewController 

var selectedOption:Data! 

// ............ 

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

    if indexPath.row == 0 { 
     selectedOption = Data.OuterDiameter 
    } 
    else if indexPath.row == 1 { 
     selectedOption == Data.Weight 
    } 
    else { 
     selectedOption = Data.Innerdiameter; 
    } 
    self.performSegueWithIdentifier("ViewController2", sender: nil) 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 

    let view2 = segue.destinationViewController as! ViewController2 
    view2.selectedData = selectedOption 
} 
} 
+0

感謝您的回覆!我會在下班回家時給你一個鏡頭,讓你知道它是怎麼回事 – Matty

+0

當我在我的viewDidLoad中輸入開關盒時,它給了我一個錯誤「enum case'OuterDiameter'在數據類型中找不到!」。 。這可以完全相關的事實,我使用tableViewController而不是使用tableView頂部的viewController? – Matty

1

這一切是不是complex.Do以下。

  • 在首先的viewController如你說「我有從上述TableViewController一個賽格瑞(模態)向第二TableViewController」給這個原因請看的標識符。

  • 在第二個ViewController上爲第一個viewncontroller中選擇的行的類型添加一個類似當前選擇的屬性。

  • 在第一次的viewController在你didselectrow方法設置當前選擇到一個變量,然後執行

(無效)prepareForSegue:(UIStoryboardSegue *)賽格瑞發件人:(ID)發送

  • 這裏從segue獲取您的視圖控制器並將屬性設置爲當前選擇

  • 在您的secondViewController的ViewWillAppear或任何其他適當的方法根據您當前選擇的屬性加載數據。

+0

謝謝伊姆蘭的回答。我有一個問題是你是什麼意思,通過添加一個屬性,如當前選擇的行在我的第一個視圖控制器中選擇的行類型? – Matty

+0

是的,可以是字符串或枚舉或任何你喜歡的 – Imran

+0

嗨伊姆蘭,我相信你說的是可愛的天使在他們對這篇文章的回答中提到的。然而,我不能讓開關工作......得到錯誤「enum case'OuterDiameter'沒有在數據類型中找到!」。有什麼建議麼? – Matty

1

斯威夫特版本。

您可以添加標籤,你的自定義單元格按鈕(你的子類它,給它標識符,並且如果它是一個筆尖你註冊的子類?),然後在的cellForRowAtIndexPath -

cell.youButton.tag = indexPath.row 
cell.youButton.addTarget(self, action: "youMethod", forControlEvents: .TouchDown) 
+0

謝謝Elkhan,當我在我的單元中有一個按鈕的階段時,它會派上用場! – Matty

相關問題