Swift: Wie verwende ich "didSelectItemAtIndexPath" in UITableViewController (enthält UICollectionView)?
Ich habe einUITableViewController
, innerhalb desTableViewCell
, es ist einUICollectionView
. Ich möchte die Daten vom @ übergebCollectionViewCell
zu einerDetailViewController
, als der Benutzer auf die Zelle tippte. Ich habe einen Abschnitt aus dem @ gezogCollectionViewCell
zumDetailViewController
und benutzte dasdidSelectItemAtIndexPath
innerhalb desTableViewCell
( was beinhaltetCollectionView
), und es funktioniert
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]
}
es kann drucken"ausgewählt" was bedeutet, dass die Daten bereits in @ gespeichert wurdself.selectedPost1
. Aber ich kann nicht @ verwendprepareForSegue
innerhalb dieser Klasse, da es nur in @ verwendet werden kaViewController
. jemand sagte mir, @ zu implementierUICollectionViewDelegate
in meinemHomeTableViewController
und rufe die Funktion @ adidSelectedItemAtIndexPath
in demHomeTableViewController
, so was
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
}
DerdidSelectedItemAtIndexPath
kann nicht aufgerufen werden, es erfolgt kein Ausdruck. Ich weiß nicht warum?
Hier ist meinDetailViewController
:
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)
}
}
und muss ich auch in @ implementierviewDidLoad
oderviewDidAppear
?
Ich habe seit ein paar Tagen mit diesem Problem zu kämpfen? brauche ein paar Vorschläge, wie man aus einem @ ausweicUICollectionViewCell
(das ist in einemUITableViewCell
) zu einem neuenUIViewController
.