我正在开发我的第一个iOS应用程序和新学生。
一切正常,但我无法弄清楚如何从特定的细胞到第三个视图控制器。我有一个IOS UITableView
有三个部分和总共(44)个细胞。拍打时所有细胞都是一个DetailVC
标题为:showProductDetai
,这很好。我遇到的问题是我需要在UITableView
的第0行第5行中只有(1)特定的单元格去它自己的ViewController
,其中我标题为:second view controller
而不是正常的showProductDetail
VC。是否有一种智能方法可以使tableView
的第0行第5行中的特定单元格在选择时转到第二个视图控制器?
这是我当前正在运行的代码。我将如何编码进行更改?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell", for: indexPath) as! ProductTableViewCell
// Configure the cell...
let productLine = productLines[indexPath.section]
let products = productLine.products
let product = products[indexPath.row]
cell.product = product
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let productLine = productLines[section]
return productLine.name
}
// Mark: UITableViewDelegate
var selectedProduct: Product?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let productLine = productLines[indexPath.section]
let product = productLine.products[indexPath.row]
selectedProduct = product
performSegue(withIdentifier: "ShowProductDetail", sender: nil)
}
// Mark: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "ShowProductDetail" {
let DetailVC = segue.destination as! DetailViewController
DetailVC.product = selectedProduct
}
}
看看我在这做了什么?您可以使用indexPath
参数来获取用户触摸的部分和行,然后您可以设置程序性segue。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if ((indexPath.section == 0) && (indexPath.row == 5)) {
performSegue(withIdentifier: "GoToSecondViewController", sender: nil)
} else {
let productLine = productLines[indexPath.section]
let product = productLine.products[indexPath.row]
selectedProduct = product
performSegue(withIdentifier: "ShowProductDetail", sender: nil)
}
}
为所需的部分和行写一个segue代码:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 && indexPath.row == 5 {
//Perform Segue
} else {
//Rest of the functionality
}
}
确保在Storyboard中连接了正确的Sugues并给出了正确的标识符。
你不需要写prepare
Segue方法