gpt4 book ai didi

ios - 如何将数据从collectionView和tableView传递到detailViewController?

转载 作者:行者123 更新时间:2023-11-30 12:39:04 25 4
gpt4 key购买 nike

我可以让segue正常工作,但是集合/ TableView Controller 中的任何数据都不会传递到详细信息(我只看到白屏。)

这是collectionViewController(名为MemeCollectionViewController)

import UIKit
import Foundation


class SentMemesCollectionViewController: UICollectionViewController {

@IBOutlet weak var flowLayout: UICollectionViewFlowLayout!

var _collectionView = SentMemesCollectionViewController!.self


//calling memes from array in Delegate
let appDelegate = UIApplication.shared.delegate as! AppDelegate
var memes: [Meme] {
return appDelegate.memes
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView?.reloadData()

flowLayoutSettings()

}
func flowLayoutSettings() {

let space: CGFloat = 3.0
let dimension = (self.view.frame.size.width - (2 * space)) / 3

flowLayout.minimumInteritemSpacing = space
flowLayout.minimumLineSpacing = space
flowLayout.itemSize = CGSize(width: dimension, height: dimension)
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return memes.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MemeCollectionViewCell", for: indexPath) as! MemeCollectionViewCell

let meme = self.memes[indexPath.item]
cell.getCellMeme(meme.memedImage)

return cell
}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MemeDetailViewController" ,
let nextScene = segue.destination as? MemeDetailViewController ,
let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: "MemeCollectionViewCell", for: IndexPath) {
let selectedMeme = memes[IndexPath]

}
}

}

名为 MemeTableViewController 的 tableViewController

    import UIKit

class SentMemesTableViewController: UITableViewController {

var _tableView: UITableView!
var memeData: [Meme] = []



//calling memes from array in Delegate
let appDelegate = UIApplication.shared.delegate as! AppDelegate
var memes: [Meme] {
return appDelegate.memes

}

override func viewWillAppear(_ animated: Bool) {
tableView.reloadData()
}


override func viewDidLoad() {
super.viewDidLoad()

tableView.isScrollEnabled = true

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == "MemeDetailViewController" ,
let _ = segue.destination as? MemeDetailViewController,
let indexPath = tableView.indexPathForSelectedRow {
let selectedMeme = memeData[indexPath.row]
_ = SentMemeImage(memedImage: selectedMeme.memedImage)
}
}


// MARK: - Table view data source

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

//navigationController!.pushViewController(MemeDetailViewController, animated: true)

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return memes.count
}

// Here it is! -----

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableViewCell = tableView.dequeueReusableCell(withIdentifier: "sentMemesTableView") as! MemeTableViewCell
let meme = memes[indexPath.row]

tableViewCell.tableViewImage.image = meme.memedImage
tableViewCell.tableViewLabel.text = "\(meme.topText)...\(meme.bottomText)"



return tableViewCell
}

// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}

// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return false

}

func deleteMemesInTableViewCell(_ index: Int) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.memes.remove(at: index)
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCellEditingStyle.delete) {
tableView.beginUpdates()
deleteMemesInTableViewCell(indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.left)
tableView.endUpdates()
}

}
}

这里是detailController,名为MemeDetailViewController

    import UIKit

class MemeDetailViewController: UIViewController {

var meme: SentMemeImage?

@IBOutlet weak var sentMemesBtn: UIBarButtonItem!
@IBOutlet weak var editBtn: UIBarButtonItem!
@IBOutlet weak var sentMemeView: UIImageView!

override func viewWillAppear(_ animated: Bool) {
displayMeme()
}




func displayMeme() {
self.sentMemeView.image = self.meme?.memedImage

}

@IBAction func launchMemeEditorViewController(_ sender: Any) {
_ = navigationController?.popViewController(animated: true)

}

//unwinding to the view before (the collectionView, or the tableView)

@IBAction func unwindVC(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController) {
self.dismiss(animated: true, completion: nil)
}
}

最佳答案

您需要使用要传递的数据更新目标 View Controller (Meme Detail ViewController),如第一个示例中所示:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MemeDetailViewController" ,
let nextScene = segue.destination as? MemeDetailViewController ,
let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: "MemeCollectionViewCell", for: IndexPath) {
let selectedMeme = memes[IndexPath]
nextScene.meme = selectedMeme

}
}

关于ios - 如何将数据从collectionView和tableView传递到detailViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42473547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com