在进行准备(用于segue)时,我在访问collectionview.tag时遇到问题。我尝试了不同的方法,但没有任何效果。
import UIKit
class DiscoverViewController: UIViewController {
let SectionHeaderHeight: CGFloat = 30
@IBOutlet weak var tableView: UITableView!
var tmdbClient = TmdbClientService()
var tableSectionTitles = ["Coming Movies","Playing"," Popular","Toprated"]
var upComing : [Movie]?
var nowPlaying : [Movie]?
var populer : [Movie]?
var topRated : [Movie]?
override func viewDidLoad() {
super.viewDidLoad()
fetchMovieTable()
}
func fetchMovieTable(){
tmdbClient.getUpComing { (movies) in
self.upComing = movies
self.tableView.reloadData()
}
tmdbClient.getNowPlaying { (movies) in
self.nowPlaying = movies
self.tableView.reloadData()
}
tmdbClient.getPopuler { (movies) in
self.populer = movies
self.tableView.reloadData()
}
tmdbClient.getTopRated { (movies) in
self.topRated = movies
self.tableView.reloadData()
}
}
}
//-MARK: TableView Delegate & Datasource
extension DiscoverViewController : UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 4
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return tableSectionTitles[section]
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
return nil
}else {
let cell = tableView.dequeueReusableCell(withIdentifier: "sectionCell") as! SectionTableViewCell
cell.setupSection(sectionTitle: tableSectionTitles[section])
return cell
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return CGFloat.leastNormalMagnitude
}
return SectionHeaderHeight
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0{
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderTableViewCell") as! HeaderTableViewCell
cell.headerCollectionView.tag = indexPath.section
return cell
}
}else if indexPath.section == 1{
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ContentTableViewCell") as! ContentTableViewCell
cell.contentCollectionView.tag = indexPath.section
return cell
}
}else if indexPath.section == 2{
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ContentTableViewCell") as! ContentTableViewCell
cell.contentCollectionView.tag = indexPath.section
return cell
}
}else if indexPath.section == 3{
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ContentTableViewCell") as! ContentTableViewCell
cell.contentCollectionView.tag = indexPath.section
return cell
}
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
if indexPath.row == 0 {
if let cell = cell as? HeaderTableViewCell {
cell.headerCollectionView.dataSource = self
cell.headerCollectionView.delegate = self
cell.headerCollectionView.reloadData()
}
}
}else if indexPath.section == 1{
if indexPath.row == 0 {
if let cell = cell as? ContentTableViewCell {
cell.contentCollectionView.dataSource = self
cell.contentCollectionView.delegate = self
cell.contentCollectionView.reloadData()
}
}
}else if indexPath.section == 2{
if indexPath.row == 0 {
if let cell = cell as? ContentTableViewCell {
cell.contentCollectionView.dataSource = self
cell.contentCollectionView.delegate = self
cell.contentCollectionView.reloadData()
}
}
}else if indexPath.section == 3{
if indexPath.row == 0 {
if let cell = cell as? ContentTableViewCell {
cell.contentCollectionView.dataSource = self
cell.contentCollectionView.delegate = self
cell.contentCollectionView.reloadData()
}
}
}
}
}
//-MARK: CollectionView Delegate & Datasource
extension DiscoverViewController : UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView.tag == 0{
if let upComingMovies = upComing{
return upComingMovies.count
}
}
if collectionView.tag == 1 {
if let nowPlayingMovies = nowPlaying{
return nowPlayingMovies.count
}
}
if collectionView.tag == 2 {
if let populerMovies = populer{
return populerMovies.count
}
}
if collectionView.tag == 3 {
if let topRatedMovies = topRated{
return topRatedMovies.count
}
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.tag == 0{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderCollectionViewCell", for: indexPath) as! HeaderCollectionViewCell
cell.movie = upComing?[indexPath.row]
return cell
}
if collectionView.tag == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCollectionViewCell", for: indexPath) as! ContentCollectionViewCell
cell.movie = nowPlaying?[indexPath.row]
return cell
}
if collectionView.tag == 2 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCollectionViewCell", for: indexPath) as! ContentCollectionViewCell
cell.movie = populer?[indexPath.row]
return cell
}
if collectionView.tag == 3 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCollectionViewCell", for: indexPath) as! ContentCollectionViewCell
cell.movie = topRated?[indexPath.row]
return cell
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Collection view at row \(collectionView.tag) selected index path \(indexPath.item)")
self.performSegue(withIdentifier: "showDiscover", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}
}
最佳答案
您可以在 self.performSegue 中将 collectionview 对象传递为:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Collection view at row \(collectionView.tag) selected index path \(indexPath.item)")
self.performSegue(withIdentifier: "showDiscover", sender: collectionView)
}
然后在prepareSegue中:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let collectionView = sender as! UICollectionView
if collectionView.tag == 0 {
//write your logic
}
if collectionView.tag == 1 {
//write your logic
}
}
这会起作用
关于ios - 当 Tableview 中嵌入了多个 Collection View 时,如何进行 Segue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47796894/