ios - 样本: Tableview of custom cells to another tableview of custom cells

标签 ios swift tableview cell

有人可以分享从一组自定义单元格切换到另一个自定义单元格的 tableView 的转换示例吗?重要的是第一个 TableView 继承了第二个 TableView 的信息。

示例:

第一个表:

[主题图片] 主题 - Name1 已按下

[主题图片]主题 - 名称2

[主题图片]主题 - 名称3

[主题图片]主题 - 名称4

第二个表:

标题标签:“主题 - 名称1”

[主题图片 - 名称 1] - 图片名称

[主题图片 - 名称 1] - 图片名称

[主题图片 - 名称 1] - 图片名称

[主题图片 - 名称 1] - 图片名称

[主题图片 - 名称 1] - 图片名称

这里可以在xCode中看到上面的例子

example

“主题”的 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)
}
}

第二个 View Controller :

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
}
}

这是实际结果:

Thread 1: Fatal error: Index out of range" in the Second ViewController at the following line: "cell.createAnimal(animal: animalArray![myIndex])

示例应该足以理解该过程。 预先感谢您的帮助!

最佳答案

请看下面的代码。看代码你应该能够理解

import UIKit

struct breedClass {
    var breed:String
    var previewImage:UIImage
    var innerData:[detailAnimals]
}

struct detailAnimals {
    var name:String
    var image:UIImage
}

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

let BreedArray : [breedClass] = [
    breedClass(breed: "Dogs", previewImage: #imageLiteral(resourceName: "banner_home_001"), innerData: [
        detailAnimals(name: "Black Dog", image: #imageLiteral(resourceName: "best_of_ott_003")),
        detailAnimals(name: "White Dog", image: #imageLiteral(resourceName: "best_of_ott_001")),
        detailAnimals(name: "Brown Dog", image: #imageLiteral(resourceName: "best_of_ott_002"))]),
    breedClass(breed: "Cats", previewImage: #imageLiteral(resourceName: "banner_home_001"), innerData: [
        detailAnimals(name: "Black Cat", image: #imageLiteral(resourceName: "best_of_ott_003")),
        detailAnimals(name: "White Cat", image: #imageLiteral(resourceName: "best_of_ott_001")),
        detailAnimals(name: "Bronw Cat", image: #imageLiteral(resourceName: "best_of_ott_002"))]),
    breedClass(breed: "Rabbits", previewImage: #imageLiteral(resourceName: "banner_home_001"), innerData: [
        detailAnimals(name: "Black Rabbit", image: #imageLiteral(resourceName: "best_of_ott_003")),
        detailAnimals(name: "White Rabbit", image: #imageLiteral(resourceName: "best_of_ott_001")),
        detailAnimals(name: "Brown Rabbit", image: #imageLiteral(resourceName: "best_of_ott_002"))])
]


@IBOutlet weak var myTV: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    myTV.register(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "MyCell")
    myTV.separatorStyle = .none

    myTV.delegate = self
    myTV.dataSource = self
    myTV.reloadData()
}


func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return BreedArray.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell
    cell.myImg.image = BreedArray[indexPath.row].previewImage
    cell.lbl_Title.text = BreedArray[indexPath.row].breed
    cell.selectionStyle = .none
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 150
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = SecondViewController()
    vc.myBreedClass = BreedArray[indexPath.row]
    self.navigationController?.pushViewController(vc, animated: true)
}

}

第二 View Controller

import UIKit

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var myBreedClass:breedClass?

@IBOutlet weak var myTV: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    myTV.register(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "MyCell")
    myTV.separatorStyle = .none

    myTV.delegate = self
    myTV.dataSource = self
    myTV.reloadData()
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if let count = myBreedClass?.innerData.count{
        return count
    }
    return 0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let lbl = UILabel()
    lbl.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: 150.0)
    lbl.text = myBreedClass?.breed ?? ""
    lbl.textAlignment = .center
    return lbl
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if let count = myBreedClass?.innerData.count{
        if count>0{
            return 150
        }
        return 0
    }
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell
    cell.myImg.image = myBreedClass?.innerData[indexPath.row].image
    cell.lbl_Title.text = myBreedClass?.innerData[indexPath.row].name
    cell.selectionStyle = .none
    return cell

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}


}

关于ios - 样本: Tableview of custom cells to another tableview of custom cells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54616420/

相关文章:

ios - 延长 dispatch_after 的调度时间

xcode - 将 GeoFire 与 Firebase 3.2.1 结合使用时出现 "Expected a type"错误

ios - Swift:跳转到 CollectionView 中的下一个文本字段

swift - 强制施放真的很糟糕并且应该始终避免吗?

javafx - CSV 到 TableView 跳过标题 (JavaFX)

objective-c - TableView 未正确更新

ios - UICollectionView 显示两行,但我只想要一行

swift - 将 TableView 嵌套到分组 TableView 中

ios - 将 subview 添加到 subview ,但来自另一个类

swift - Sprite 套件中的接触检测