Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:如何使用;didSelectItemAtIndexPath“;在UITableViewController(包含UICollectionView)中?_Ios_Swift_Uitableview_Uicollectionview_Segue - Fatal编程技术网

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

Ios Swift:如何使用;didSelectItemAtIndexPath“;在UITableViewController(包含UICollectionView)中?,ios,swift,uitableview,uicollectionview,segue,Ios,Swift,Uitableview,Uicollectionview,Segue,我有一个UITableViewController,在TableViewCell中,它是一个UICollectionView。当用户点击单元格时,我想将数据从CollectionViewCell传递到DetailViewController。我从CollectionViewCell拖动一段到DetailViewController,并在TableViewCell(包含CollectionView)中使用didSelectItemAtIndexPath),它可以工作 class TableView

我有一个
UITableViewController
,在
TableViewCell
中,它是一个
UICollectionView
。当用户点击单元格时,我想将数据从
CollectionViewCell
传递到
DetailViewController
。我从
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
中。但是我不能在这个类中使用
prepareforsgue
,因为它只能在
ViewController
中使用。有人告诉我在我的
HomeTableViewController
中实现
UICollectionViewDelegate
,并在
HomeTableViewController
中调用函数
DIDSelectedeMatindexPath
,如下所示:

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

}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell  
{
....
myButton.tag = indexpath.item
}
我还需要在
viewDidLoad
viewDidAppear
中实现吗


我在这个问题上挣扎了几天?需要一些关于如何从
UICollectionViewCell
(位于
UITableViewCell
内)转换到新的
UIViewController
的建议,因此,您尚未实现

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

方法在第二个实现中。 另外,尝试在collection view单元格上放置一个不可见按钮,并将标记指定为该collectionview单元格的indexpath,如下所示:

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

}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell  
{
....
myButton.tag = indexpath.item
}
在此之后,您可以实现从collectionviewcell类到homepagetableviewcontroller类的委托回调,或者通过代码直接推送详细视图控制器


就细节视图控制器中的图像设置而言。您可以在viewdidLoad()或ViewDidDisplay()中执行此操作。很好。

你的方法是正确的,但你正在制造一个错误。尝试在
TableViewCell
中实现
UICollectionViewDelegate
方法
didSelectItemAtIndexPath
并将其从
主页TableViewController
中删除

现在声明一个协议,然后在
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

class HomePageTableViewController: UITableViewController,UICollectionViewDelegate { 


     //Your code 

     func selectedPost(post: Posts) {
         //You will get your post object here, do what you want now
     }
}
注意:
cellforrowatinexpath
内不要忘记用
HomePageTableViewController
设置
postDelgate
的委托

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
}

对于didSelectRowAtIndexPath内的collectionview,删除segue并更新我,如果它确实打印“已选”我删除了segue,并且在我点击Cell后它仍然可以打印“已选”,我建议您参考与您需要的实现相同的segue。虽然在您的代码中,您需要使用
cell.objectOfCollectionview
cellForRowAtIndexPath
中设置
UICollectionView
委托。好的,我来看看@DipenPanchasara。因为我是swift新手,还有很多东西不明白。它是用objective-c编写的,我对它不太熟悉,我开始用swift学习IOS开发。@DipenPanchasara所以你的意思是我必须在HomeTableViewController上实现“NumberOfItemInstation”和“cellForItemAtIndexPath”?我为什么要这么做?我需要返回多少号码?对于单元,我如何实现它?抱歉,我是Swift新手。这不是关于Swift的,但一般来说,每个“NumberOfItemInstruction”都会调用一个“cellForItemAtIndexPath”。因此,如果节中的项目数为0(或未提及)。细胞没有形成。因此,“didSelectRowAtIndexPath”不会有任何效果。但是在我的HomeTableViewController中,我有numberOfRowAtIndexPath和cellForRowAtIndexPath。但是如果我使用didselectedrowatinexpath,它就不起作用了。为什么呢?谢谢,我是Swift新手,不太熟悉创建新协议。你是说我需要在TableViewCell中实现“protocol PostDelegate”吗?然后,我把这个协议放在HomePageTableViewController中?我已从HomePageTableViewController中删除了“DidSelectedEditeMatIndexPath”,是否仍需要此HomePageTableViewController中的“func prepareForSegue”“?在
TableViewCell
外部声明协议,而不是在内部声明,在
TableViewCell
内部创建该协议的实例,就像我的回答一样。你可以在
TableViewCell
类上面声明
protocol
。我已经按照你说的实现了该协议。现在在HomePageTableView中,我应该怎么做?我需要预约吗?在
func selectedPost(post:Posts){}
中,我应该放什么?感谢故事板如果您已将segue连接到
tablecell
,请将其删除,并在两个viewController(而不是Tabelcell和viewController)之间创建一个。在该调用之后,在selectedPost方法内执行检查。