Ios Swift:如何使用;“准备表格”;在UICollectionViewCell内部?
我在UITableView中有一个UICollectionView,我想在用户点击UICollectionViewCell时链接另一个ViewController。在UITableViewCell中,我从Internet下载了所有数据并存储在一个数组中。我通过控件创建了一个Segue,将CollectionViewCell拖动到新的ViewController并选择“推”。在新的ViewController中,我添加了UIImageView的IBOutlet,以在用户点击缩略图时显示CollectionViewCell的图像。现在,当我点击CollectionViewCell时,它会转到新的ViewController,但没有显示图片。这是我的代码,有什么建议吗 在UITableViewConroller中:Ios Swift:如何使用;“准备表格”;在UICollectionViewCell内部?,ios,uitableview,uicollectionview,segue,Ios,Uitableview,Uicollectionview,Segue,我在UITableView中有一个UICollectionView,我想在用户点击UICollectionViewCell时链接另一个ViewController。在UITableViewCell中,我从Internet下载了所有数据并存储在一个数组中。我通过控件创建了一个Segue,将CollectionViewCell拖动到新的ViewController并选择“推”。在新的ViewController中,我添加了UIImageView的IBOutlet,以在用户点击缩略图时显示Collec
import UIKit
class HomePageTableViewController: UITableViewController {
let sections: NSArray = ["latest news", "good news"]
var posts1: [Posts] = [Posts]()
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
}
我从JSON下载所有数据,并将它们放入这些数组中。在这个类中,我还尝试调用函数:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("selected")
self.selectedPost1 = self.posts1
}
当我点击一个单元格时,打印工作正常,但我不能在这个函数中使用“prepareForSegue”。我检查并发现该功能只能在ViewController中使用,但我如何才能做到这一点
在新的ViewController中,以下是代码:
import UIKit
import Alamofire
import AlamofireImage
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!
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var postImageView: UIImageView!
var posts: [Posts] = [Posts]()
var selectedPost: Posts?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
if let post = self.selectedPost{
let thumbnailUrlString = post.postThumbnailUrlString
let imageUrl: NSURL = NSURL(string: thumbnailUrlString)!
print(thumbnailUrlString)
postImageView.af_setImageWithURL(imageUrl)
}
}
}
因为当我下载数据时,我把它们放在两个不同的数组中,posts1和posts2。我想在新的ViewController中显示相应的图像。但我刚刚创建了一个“selectedPost”数组,我不确定这是否是问题所在?我不确定是否将数据重新加载到“selectedPost”数组,也许这就是为什么没有显示图像的原因?真的希望有人能给我一些建议。谢谢您应该使用
prepareforsgue:
。您需要做的是在选择单元格时设置一个变量selectedIndexPath
。然后在prepareforsgue:
中,您可以访问单元格及其属性,即:
HomePageTableViewController: UITableViewController {
var selectedIndexPath: NSIndexPath?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedIndexPath = indexPath
}
public override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
super.prepareForSegue(segue, sender: sender)
let cell = theCollectionView.cellForItemAtIndexPath(selectedIndexPath)
//do what you need to do
}
}
您应该使用
prepareforsgue:
。您需要做的是在选择单元格时设置一个变量selectedIndexPath
。然后在prepareforsgue:
中,您可以访问单元格及其属性,即:
HomePageTableViewController: UITableViewController {
var selectedIndexPath: NSIndexPath?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedIndexPath = indexPath
}
public override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
super.prepareForSegue(segue, sender: sender)
let cell = theCollectionView.cellForItemAtIndexPath(selectedIndexPath)
//do what you need to do
}
}
CollectionView位于TableView单元格内,我无法在HomePageTableView控制器中调用它。我需要做什么?谢谢,我明白了。您的tableView是否需要成为collectionView的代理?如果没有,您可以将viewController设置为collectionView的代理,即在
collectionView:cellForItemAtIndexPath:
settheCollectionView.delegate=self.delegate?
中,我将TableView拖到TableViewController的数据源并进行代理。并拖动TableViewCell作为CollectionView的数据源和委托。所以我想我需要TableView作为CollectionView的代理。那我该怎么办?此外,由于我将segue从CollectionViewCell拖动到新的ViewController,即使我没有添加“prepareForSegue”,当我点击单元格时,它也会转到新的ViewController。所以我很困惑。谢谢您是否可以将ViewController设置为CollectionView的委托(不是数据源)?我可以将CollectionView拖到HomeTableViewController,然后选择委托。抱歉,我是swift新手,因此仍在学习,不确定我是否做得对。集合视图位于TableView单元格内,我无法在HomePageTableViewController中调用它。我需要做什么?谢谢,我明白了。您的tableView是否需要成为collectionView的代理?如果没有,您可以将viewController设置为collectionView的代理,即在collectionView:cellForItemAtIndexPath:
settheCollectionView.delegate=self.delegate?
中,我将TableView拖到TableViewController的数据源并进行代理。并拖动TableViewCell作为CollectionView的数据源和委托。所以我想我需要TableView作为CollectionView的代理。那我该怎么办?此外,由于我将segue从CollectionViewCell拖动到新的ViewController,即使我没有添加“prepareForSegue”,当我点击单元格时,它也会转到新的ViewController。所以我很困惑。谢谢您是否可以将ViewController设置为CollectionView的委托(不是数据源)?我可以将CollectionView拖到HomeTableViewController,然后选择委托。对不起,我是swift新手,所以仍在学习,不确定我是否做对了。