我有一个UITableViewController,在TableViewCell内,它是UICollectionView。我想在用户点击单元格时将数据从CollectionViewCell传递到DetailViewController。我将segue从CollectionViewCell拖到DetailViewController,并在didSelectItemAtIndexPath(包含TableViewCell)中使用了CollectionView,并且可以正常工作。

class TableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {

var posts1: [Posts] = [Posts]()
var posts2: [Posts] = [Posts]()
var categories: [Category] = [Category]()
var selectedPost1: Posts?

@IBOutlet weak var theCollectionView: UICollectionView!


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return posts1.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let collectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell

        let imageUrlString: String = posts1[indexPath.row].postThumbnailUrlString
        let imageUrl: NSURL = NSURL(string: imageUrlString)!
    //Give Images A round cornor

    let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
        size: collectionViewCell.postImageView.frame.size,
        radius: 20.0
    )

    collectionViewCell.postImageView.af_setImageWithURL(imageUrl, filter: filter)

    collectionViewCell.postTitleLabel.text = posts1[indexPath.row].postTite
return collectionViewCell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("selected")
    self.selectedPost1 = self.posts1[indexPath.row]
}

它可以打印“已选择”,这意味着数据已经存储在self.selectedPost1中。但是我不能在此类中使用prepareForSegue,因为它只能在ViewController中使用。有人告诉我在UICollectionViewDelegate中实现HomeTableViewController,然后在didSelectedItemAtIndexPath中调用HomeTableViewController函数,如下所示:
import UIKit

class HomePageTableViewController: UITableViewController,UICollectionViewDelegate {
    let sections: NSArray = ["latest news", "good news"]
    var posts1: [Posts] = [Posts]()
    var selectedIndexPath: NSIndexPath?

override func viewDidLoad() {
    super.viewDidLoad()


}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  if section < sections.count{
        return sections[section] as? String
    }

    return nil

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return sections.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row == 0 && indexPath.section == 0 {
        let tableViewCell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as! TableViewCell

        tableViewCell.getCategories()
     //   tableViewCell.scrollToNextCell()
     //   tableViewCell.startTimer()


        return tableViewCell

    }
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("goToDetail", sender: TableViewCell())
    print("selected")
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let tableViewCell1 = sender as? TableViewCell,
    let postDetailPage = segue.destinationViewController as? DetailViewController{

        let selectedPosts = tableViewCell1.selectedPost1

        postDetailPage.selectedPost?.selectedPost1 = selectedPosts

}

但是,无法调用didSelectedItemAtIndexPath,无法打印。不知道为什么

这是我的DetailViewController:
import UIKit

class DetailViewController: UIViewController {
@IBOutlet weak var postImageView: UIImageView!

var posts: [Posts] = [Posts]()
var selectedPost: TableViewCell?

override func viewDidLoad() {
    super.viewDidLoad()

    if let post = selectedPost?.selectedPost1{
        let thumbnailUrlString = post.postThumbnailUrlString
        let imageUrl: NSURL = NSURL(string: thumbnailUrlString)!
        print(thumbnailUrlString)
        postImageView.af_setImageWithURL(imageUrl)
    }
}

而且我还需要实现viewDidLoadviewDidAppear吗?

我在这个问题上苦苦挣扎了几天?需要有关如何从UICollectionViewCell(位于UITableViewCell内部)进行Segue到新的UIViewController的一些建议。

最佳答案

您的操作方法是正确的,但是却造成了一个错误。尝试在UICollectionViewDelegate中也实现didSelectItemAtIndexPath方法TableViewCell并将其从HomePageTableViewController中删除。

现在声明一个协议,然后在TableViewCell中创建其实例,并在HomePageTableViewController中实现该协议,然后在didSelectItemAtIndexPath中使用该委托实例来调用这种方法。

protocol PostDelegate {
    func selectedPost(post: Posts)
}

现在在TableViewCell中创建其实例,并在didSelectItemAtIndexPath中调用此委托方法。
class TableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
     var posts1: [Posts] = [Posts]()
     var posts2: [Posts] = [Posts]()
     var categories: [Category] = [Category]()
     var selectedPost1: Posts?
     var postDelegate: PostDelegate?

     func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
         postDelegate?. selectedPost(self.posts1[indexPath.row])
     }
}

现在在PostDelegate中实现此HomePageTableViewController协议
class HomePageTableViewController: UITableViewController,UICollectionViewDelegate {


     //Your code

     func selectedPost(post: Posts) {
         //You will get your post object here, do what you want now
     }
}

注意:cellForRowAtIndexPath内部,不要像这样用您的postDelgate设置HomePageTableViewController的委托
tableViewCell.postDelegate = self

编辑:
func selectedPost(post: Posts) {
    //You will get your post object here, do what you want now
    self.performSegueWithIdentifier("goToDetail", sender: post)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let post = sender as! Posts
    let postDetailPage = segue.destinationViewController as? DetailViewController
    postDetailPage.passPost = post
}

关于ios - Swift:如何在UITableViewController(包含UICollectionView)中使用“didSelectItemAtIndexPath”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39030309/

10-12 05:39