首页 文章

Swift:如何在UITableViewController中使用“didSelectItemAtIndexPath”(包含UICollectionView)?

提问于
浏览
2

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

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 ,如下所示:

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 内)到一个新的 UIViewController 的Segue的建议 .

2 回答

  • 0

    你的方式是正确的,但你正在创造一个错误 . 尝试在 TableViewCell 内实现 UICollectionViewDelegate 方法 didSelectItemAtIndexPath 并将其从 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])
         }
    }
    

    现在在 HomePageTableViewController 内实现这个 PostDelegate 协议

    class HomePageTableViewController: UITableViewController,UICollectionViewDelegate { 
    
    
         //Your code 
    
         func selectedPost(post: Posts) {
             //You will get your post object here, do what you want now
         }
    }
    

    Note: 里面 cellForRowAtIndexPath 别忘了用你的 HomePageTableViewController 设置 postDelgate 的代表

    tableViewCell.postDelegate = self
    

    Edit:

    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
    }
    
  • 1

    所以,你还没有实现

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

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

    第二个实现中的方法 . 另外,尝试在集合视图单元格上放置一个不可见的按钮,并将标记指定为该集合视图单元格的索引路径,如下所示:

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell  
    {
    ....
    myButton.tag = indexpath.item
    }
    

    在此之后,您可以实现从collectionviewcell类到homepagetableviewcontroller类的委托回调,也可以直接按代码推送详细视图控制器 .

    至于详细视图控制器中的图像设置 . 您可以在viewdidLoad()或viewDidAppear()中执行此操作 . 没关系 .

相关问题