我在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)
}
}
我还需要在viewDidLoad
或viewDidAppear
?
这几天我一直在努力解决这个问题?需要一些关于如何从UICollectionViewCell
(在UITableViewCell
内)到新UIViewController
进行搜索的建议。
答案 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()中执行此操作。很好。