SWIFT UITableView - 在原型单元格中的多行继续到另一个UITableView
我真的不知道要搜索什么关于这个问题,我的搜索尝试没有太多的帮助,所以我想我会给这个帖子一个镜头。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
}
*/
首先去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
}
}
这一切是不是complex.Do以下。
在首先的viewController如你说“我有从上述TableViewController一个赛格瑞(模态)向第二TableViewController”给这个原因请看的标识符。
在第二个ViewController上为第一个viewncontroller中选择的行的类型添加一个类似当前选择的属性。
在第一次的viewController在你didselectrow方法设置当前选择到一个变量,然后执行
(无效)prepareForSegue:(UIStoryboardSegue *)赛格瑞发件人:(ID)发送
这里从segue获取您的视图控制器并将属性设置为当前选择
在您的secondViewController的ViewWillAppear或任何其他适当的方法根据您当前选择的属性加载数据。
斯威夫特版本。
您可以添加标签,你的自定义单元格按钮(你的子类它,给它标识符,并且如果它是一个笔尖你注册的子类?),然后在的cellForRowAtIndexPath -
cell.youButton.tag = indexPath.row
cell.youButton.addTarget(self, action: "youMethod", forControlEvents: .TouchDown)
谢谢Elkhan,当我在我的单元中有一个按钮的阶段时,它会派上用场! – Matty
感谢您的回复!我会在下班回家时给你一个镜头,让你知道它是怎么回事 – Matty
当我在我的viewDidLoad中输入开关盒时,它给了我一个错误“enum case'OuterDiameter'在数据类型中找不到!”。 。这可以完全相关的事实,我使用tableViewController而不是使用tableView顶部的viewController? – Matty