问题描述
我有一个UITableViewController,在TableViewCell中,它是一个UICollectionView。我想在用户点击单元格时将CollectionViewCell中的数据传递给DetailViewController。我将一个segue从CollectionViewCell拖到DetailViewController,并在TableViewCell(包含CollectionView)中使用了didSelectItemAtIndexPath,它可以工作。
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]
}
它可以打印已选择,这意味着数据已经存储在 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:
here is my 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)
}
}
我还需要在viewDidLoad或viewDidAppear中实现吗?
and also do I need to implement in viewDidLoad or viewDidAppear?
我几天来一直在这个问题上挣扎?需要一些关于如何从UICollectionViewCell(在UITableViewCell内部)到新的UIViewController进行Segue的建议。非常感谢。
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. thanks so much.
推荐答案
你的方式是正确的,但是你创造了一个错误。试着实施 UICollectionViewDelegate
方法 didSelectItemAtIndexPath
也在 TableViewCell
内,并从<$ c中删除它$ C> 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
中创建其实例并在<$ c $中实现协议c> 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 $ c $中调用此委托方法c>。
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中实现这个
PostDelegate
协议
Now implement this PostDelegate
protocol inside 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
这样
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)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!