示例:自定义单元格的表视图到自定义单元格的另一个表视图

时间:2019-02-10 12:34:41

标签: ios swift tableview cell

有人可以共享从一组自定义单元格转换到另一个自定义单元格tableView的过渡示例吗?重要的是第一个表视图将信息继承到第二个表视图。

示例:

第一张桌子:

[主题图片]主题-名称1被按下

[主题图片]主题-Name2

[主题图片]主题-Name3

[主题图片]主题-Name4

第二张表:

Header-Label:“主题-名称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)
}
}

第二个ViewController:

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:   animalArray![myIndex])

一个样本应该足以理解该过程。 预先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

请参见下面的代码。您应该能够看懂代码

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

}

SecondViewController

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
}


}