【发布时间】:2018-05-19 23:52:26
【问题描述】:
我在 TableViewCell 中有 collectionView。 TableView 有 1 节和 4 行。
我想知道当collectionView水平滚动时选择了tableviewcell的哪一行。
我试图通过将 tableView 行放入“var nowRow”变量中来弄清楚,如下面的代码。但它不起作用。
我怎样才能知道tableviewcell的哪一行被选中了?
class Home2ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate {
var posts = [Post]()
var nowRow = Int()
override func viewDidLoad() {
// get posts elements here
//posts.append(element).......
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count // 4counts
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let cell = DetailMutiTableViewCell()
let cell = tableView.dequeueReusableCell(withIdentifier: "DetailMutiTableViewCell", for: indexPath) as! DetailMutiTableViewCell
nowRow = indexPath.row
cell.post = posts[nowRow]
cell.collectionView1.delegate = self
cell.collectionView1.dataSource = self
cell.collectionView1.reloadData()
return cell
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts[nowRow].imageUrls.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MultiImagesCollectionViewCell", for: indexPath) as! MultiImagesCollectionViewCell
let photoUrlString = posts[nowRow].imageUrls[indexPath.item] // ←error occured
let photoUrl = URL(string: photoUrlString)
cell.imageView1.sd_setImage(with: photoUrl)
return cell
}
}
编辑编辑
【问题讨论】:
-
手动将其存储在您的视图控制器中
-
indexPathForSelectedRow的UITableView属性怎么样?还是实现UITableViewDelegate协议的选择方法? -
@McNight,与
collectionView交互不会选择包含它的tableView 单元格,所以indexPathForSelectedRow是nil并且tableView(_:didSelectRowAt:)不会被调用。
标签: ios swift swift3 tableview collectionview