Swift:如何使用“didSelectItemAtIndexPath";在 UITableViewController(包含 UICollectionView)? [英] Swift: How to use "didSelectItemAtIndexPath" in UITableViewController(contains UICollectionView)?

查看:35
本文介绍了Swift:如何使用“didSelectItemAtIndexPath";在 UITableViewController(包含 UICollectionView)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableViewController,在TableViewCell里面,它是一个UICollectionView.当用户点击单元格时,我想将数据从 CollectionViewCell 传递到 DetailViewController .我将 CollectionViewCell 中的一个 segue 拖到 DetailViewController 中,并在 TableViewCell 中使用了 didSelectItemAtIndexPath(其中包含 <代码>CollectionView),它的工作原理.

I have a UITableViewController, inside the TableViewCell, it's a UICollectionView. I want to pass the data from the CollectionViewCell to a DetailViewController when user tapped the cell. I dragged a segue from the CollectionViewCell to the DetailViewController, and used the didSelectItemAtIndexPath inside the TableViewCell( which contains CollectionView), and it works.

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]
}

它可以打印selected",这意味着数据已经存储在self.selectedPost1中.但是我不能在这个类中使用prepareForSegue,因为它只能在ViewController中使用.有人告诉我在我的 HomeTableViewController 中实现 UICollectionViewDelegate,并在 HomeTableViewController 中调用函数 didSelectedItemAtIndexPath,如下所示:

it can print "selected" which means the data have already been stored in self.selectedPost1. But I can't use prepareForSegue inside this class, since it can only be used in ViewController. someone told me to implement UICollectionViewDelegate in my HomeTableViewController, and call the function didSelectedItemAtIndexPath in the HomeTableViewController, like this:

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 无法调用,没有打印.我不知道为什么?

However, the didSelectedItemAtIndexPath can't be called, there is no print. I don't know why?

这是我的 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 中实现吗?

and also do I need to implement in viewDidLoad or viewDidAppear?

我这几天一直在为这个问题苦苦挣扎?需要一些关于如何从 UICollectionViewCell(位于 UITableViewCell 内)到新的 UIViewController 的建议.

I have been struggled in this problem for few days? need some suggestions on How to do Segue from a UICollectionViewCell (which is inside a UITableViewCell) to a new UIViewController.

推荐答案

你走的路是对的,但是你犯了一个错误.尝试实现UICollectionViewDelegate方法didSelectItemAtIndexPath 也在 TableViewCell 中,并将其从 HomePageTableViewController 中删除.

The way you are going is correct, but you are creating one mistake.Try to implement UICollectionViewDelegate method didSelectItemAtIndexPath also inside TableViewCell and remove it from the HomePageTableViewController.

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

Now declare one protocol, after that create its instance inside TableViewCell and implement the protocol in the HomePageTableViewController, after that in the didSelectItemAtIndexPath use that delegate instance to call the method like this.

protocol PostDelegate {
    func selectedPost(post: Posts)
}

现在在 TableViewCell 中创建它的实例,并在 didSelectItemAtIndexPath 中调用这个委托方法.

Now create its instance inside TableViewCell and call this delegate method in the 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])
     }
}

现在在 HomePageTableViewController

class HomePageTableViewController: UITableViewController,UICollectionViewDelegate { 


     //Your code 

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

注意:cellForRowAtIndexPath 内部不要忘记像这样使用 HomePageTableViewController 设置 postDelgate 的委托

Note: Inside cellForRowAtIndexPath don't forgot to set the delegate of postDelgate with your HomePageTableViewController like this

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
}

这篇关于Swift:如何使用“didSelectItemAtIndexPath";在 UITableViewController(包含 UICollectionView)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆