样本:定制单元的泰伯维定制细胞的另一种实现代码如下

问题描述 投票:0回答:1

可能有人共享来自一组自定义的细胞,切换到定制单元的另一个的tableView一个过渡的样品?重要的是,第一个表视图继承信息到第二的tableView。

例:

第一个表:

主题[一个主题的照片] - 名1压

[一个主题的照片]主题 - 名称2

[一个主题的照片]主题 - NAME3

[一个主题的照片]主题 - NAME4

第二个表:

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

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

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

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

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

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

在这里,您可以看到在Xcode上面的例子

example

在“主题”的视图控制器:

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(动物:animalArray [myIndex])

样本应该是足够的了解过程。预先感谢您的任何帮助!

ios swift tableview cell
1个回答
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
}


}
© www.soinside.com 2019 - 2024. All rights reserved.