Swift:如何使用" didSelectItemAtIndexPath"在UITableViewController(包含UICollectionView)?

时间:2016-08-19 02:39:15

标签: ios swift uitableview uicollectionview segue

我在UITableViewController内有TableViewCell,它是UICollectionView。我想在用户点按单元格时将数据从CollectionViewCell传递到DetailViewController。我将CollectionViewCell中的一个segue拖到了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内)到新UIViewController进行搜索的建议。

2 个答案:

答案 0 :(得分:1)

您前进的方式是正确的,但您创建了一个错误。尝试在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
}

答案 1 :(得分:0)

所以,你还没有实现

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

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell  
{
....
myButton.tag = indexpath.item
}
第二个实现中的

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

<dependency>
  <groupId>com.company</groupId>
  <artifactId>models</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/src/main/libs/models-templates-1.0.0.jar</systemPath>
</dependency>

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

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