【发布时间】:2019-02-10 12:34:40
【问题描述】:
有人可以分享从一组自定义单元格切换到另一个自定义单元格的 tableView 的转换示例吗?重要的是第一个表视图将信息继承给第二个表视图。
示例:
第一个表:
[主题图片] 主题 - Name1 按下
[主题图片] 主题 - 名称2
[主题图片] 主题 - 名称3
[主题图片] 主题 - Name4
第二桌:
标题标签:“主题 - 名称 1”
[主题图片 - Name1] - 图片名称
[主题图片 - Name1] - 图片名称
[主题图片 - Name1] - 图片名称
[主题图片 - Name1] - 图片名称
[主题图片 - Name1] - 图片名称
这里你可以在xCode中看到上面的例子
“主题”的ViewController:
import UIKit
var myIndex = 0
let BreedArray : [breedClass] = [
breedClass(breed: "Dogs", previewImage: #imageLiteral(resourceName: "Chesapeake-Bay-Retriever-1"), innerData: [
detailAnimals(image: #imageLiteral(resourceName: "black-dog"), name: "Black Dog"),
detailAnimals(image: #imageLiteral(resourceName: "white-korean-jindo-800x540"), name: "White Dog"),
detailAnimals(image: #imageLiteral(resourceName: "Chesapeake-Bay-Retriever-1"), name: "Brown Dog")]),
breedClass(breed: "Cats", previewImage: #imageLiteral(resourceName: "havana-brown-cat"), innerData: [
detailAnimals(image: #imageLiteral(resourceName: "_99805744_gettyimages-625757214"), name: "Black Cat"),
detailAnimals(image: #imageLiteral(resourceName: "twenty20_e47b3798-dd9b-40b1-91ef-1d820337966e-5aa3f798642dca00363b0df1"), name: "White Cat"),
detailAnimals(image: #imageLiteral(resourceName: "havana-brown-cat"), name: "Bronw Cat")]),
breedClass(breed: "Rabbits", previewImage: #imageLiteral(resourceName: "800px_COLOURBOX8096964"), innerData: [
detailAnimals(image: #imageLiteral(resourceName: "800px_COLOURBOX8096964"), name: "Black Rabbit"),
detailAnimals(image: #imageLiteral(resourceName: "white-rabbit-500x500"), name: "White Rabbit"),
detailAnimals(image: #imageLiteral(resourceName: "22547466-isolated-image-of-a-brown-bunny-rabbit-"), name: "Brown Rabbit")])
]
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return BreedArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! breeds
cell.createBreeds(breeds : BreedArray[indexPath.row])
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
let vc = ViewController()
vc.animalArray = BreedArray[indexPath.row].innerData
performSegue(withIdentifier: "segue", sender: self)
}
}
第二个视图控制器:
import UIKit
class ViewController: UIViewController {
var animalArray:[detailAnimals] = []
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return [animalArray].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "animalCell") as! detailAnimal
cell.createAnimal(animal: animalArray[indexPath.row])
return cell
}
}
这是实际结果:
第二个中的线程 1:致命错误:索引超出范围 ViewController 在以下行:“cell.createAnimal(animal: 动物数组![myIndex])
一个样本应该足以理解这个过程。 提前感谢您的帮助!
【问题讨论】:
-
你试过什么?什么没有按您的预期工作?
-
您是在问如何将信息从一个 tableview 传递到另一个 tableview?
-
我已经编辑了我的问题,希望我的问题变得更加清晰。