gpt4 book ai didi

ios - 如何从自定义 CollectionViewController 的 didSelectItemAtIndexPath 实例化 ViewController,它是 tableViewCell 的一部分

转载 作者:行者123 更新时间:2023-11-28 21:25:34 26 4
gpt4 key购买 nike

我想从自定义 tableViewCell(符合 UICollectionViewDelegate, UICollectionDatasource)类实例化一个 ViewController有一个自定义的 CollectionView 嵌入其中。选择 CollectionView 的特定部分时需要执行 segue。已经尝试使用协议(protocol)不起作用!

my custom TableView class :-
import UIKit


protocol transferDelegate{

func transfer(_: Int)

}


class ParentTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource{



@IBOutlet weak var parentCellHeader: UIView!

@IBOutlet weak var feedPostUsername: UILabel!

@IBOutlet weak var feedPostUserDetails: UILabel!

@IBOutlet weak var feedPostDescription: UILabel!

@IBOutlet weak var feedPicturesCollectionView: UICollectionView!

@IBOutlet weak var feedUserProfilePictures: CustomProfilepicture!



var transferingDelegate : transferDelegate?

override func awakeFromNib() {


super.awakeFromNib()

feedPicturesCollectionView.dataSource = self
feedPicturesCollectionView.delegate = self
feedPicturesCollectionView.pagingEnabled = true
feedPicturesCollectionView.backgroundColor = UIColor.clearColor()

}




override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

}




required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

}




func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

return 1

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return 10

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->
UICollectionViewCell {

let cell = feedPicturesCollectionView.dequeueReusableCellWithReuseIdentifier("FeedPicturesCell", forIndexPath: indexPath) as! FeedPhotosCell



switch(indexPath.row){

case 0 : cell.feedImages.image = UIImage(named: "defaultProfilePic")

case 1 : cell.feedImages.image = UIImage(named: "image1")

case 2 : cell.feedImages.image = UIImage(named: "image2")

case 3 : cell.feedImages.image = UIImage(named: "image3")


default : cell.feedImages.image = UIImage(named: "defaultProfilePic")

}

return cell


}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

print(indexPath.row)

transferingDelegate?.transfer(indexPath.row)


}


}

我的 viewController 类:-

import UIKit

class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource,transferDelegate{

var xibName : String = "HomepageFeedCellHeader"


var lords : [String] = ["name1","name2","name3"]

var que : [String] = ["--","red","blue"]

var desc : [String] = ["abcE","defn","ghi"]

var menProfilePictures : [UIImage] = [UIImage(named: "image1")!,UIImage(named: "image2")!,UIImage(named: "image3")!]

@IBOutlet weak var parentTableView: UITableView!


var a : ParentTableViewCell = ParentTableViewCell()






override func viewDidLoad() {

super.viewDidLoad()



parentTableView.delegate = self
parentTableView.dataSource = self

// Do any additional setup after loading the view, typically from a nib.
}


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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = parentTableView.dequeueReusableCellWithIdentifier("ParentCell", forIndexPath: indexPath) as! ParentTableViewCell
a.transferingDelegate = self
cell.feedPostUsername.text = lords[indexPath.row]
cell.feedPostUserDetails.text = queens[indexPath.row]
cell.feedPostDescription.text = desc[indexPath.row]
cell.feedUserProfilePictures.image = menProfilePictures[indexPath.row]

return cell
}

func transfer(itemNo : Int) {

print("call recieved in viewController from item \(itemNo)")

}



}

最佳答案

您的didSelectItemAtIndexPath 问题已解决。这是您在评论中提到的第二个问题的答案。如果图像不在 navigation 层次结构中,则不能将图像分配给 imageView。你的问题是你 detailImage 是 nil 因为这个 ViewController 没有加载到窗口层次结构中。要解决您的问题,请这样做

imagePopOverScene?.selImage =  menProfilePictures[itemNo]

现在像这样在你的 imagePopOverScene 中声明 selImage

var selImage: UIImage!

同时在 imagePopOverSceneVCviewDidLoad 中添加以下行

self.detailImage.image = self.selImage

这将解决你的问题

关于ios - 如何从自定义 CollectionViewController 的 didSelectItemAtIndexPath 实例化 ViewController,它是 tableViewCell 的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38271077/

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