![](/img/trans.png)
[英]How can I get IndexPath of currently selected tableviewcell by voice over?
[英]Can’t get indexPath of TableViewCell in nestled CollectionView
我有一個tableView與每個tableViewCell中嵌套的collectionViews。 每個tableViewCell是電影類型,collectionViewCells是電影。 我試圖告訴collectionView哪個TableView indexPath,以便它可以顯示正確的數據。
我遇到的問題是,當您滾動瀏覽collectionView(和TableView)時,數字會發生變化(我在collectionView單元格中有一個標簽,顯示TableView indexPath.row)。 同樣,如果我在第一個tableView單元格中滾動到collectionView的末尾,則第4個左右的tableViewCell也會滾動到末尾,就像它們被“鏈接”一樣。
為了獲得嵌套的布局,我遵循此教程https://youtu.be/4XnXHov2lQU,我最初是通過TableViewCell類設置collectionView的,但是如視頻中所述,這不符合MVC。
要獲取tableViewIndexPath
我只需在TableView的cellForRowAt
設置一個變量,然后在collectionView中將標簽設置為此。
這樣做的正確方法是什么,因為我已經看到其他問題並對此進行了回答,但是幾乎所有問題都存在相同的問題。
編輯 -TableViewController.swift
class TableViewController: UITableViewController {
var tableViewIndexPath = Int()
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 230
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! tableViewCellClass
tableViewIndexPath = indexPath.row
cell.collectionView.dataSource = self
cell.collectionView.reloadData()
return cell
}
}
extension TableViewController : UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)
let title = cell.viewWithTag(4) as! UILabel
title.text = String(tableViewIndexPath)
return cell
}
}
TableViewCellClass.swift
class tableViewCellClass: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
var indexPathForCell: IndexPath?
override func prepareForReuse() {
self.collectionView.contentOffset = .zero
self.collectionView.reloadData()
}
}
您的問題顯然缺少代碼,但是從您的描述來看,我想您在單元重用方面存在問題。
假設您將UICollectionView
滾動到右側的第一個單元格。 如果在那之后,您開始向下滾動UITableView
,則將撤消UITableViewCells
。 因此,您的4th
UICollectionView
實際上將是已被重用的第一個單元格。 Reused
意味着它是同一單元格。 而且,由於您將第一個單元格向右滾動,因此其CollectionView
具有相同的contentOffset
值,並且看起來像在滾動一樣。
每個可重復使用的單元格都有一個可以覆蓋的prepateForReuse()
方法。 通過這種方法,您可以將所有單元格參數重置為默認值。 例如,我假設在表視圖單元格類中有一個collectionView
對象。 在這種情況下,您需要下一步
override func prepareForReuse() {
self.collectionView.contentOffset = .zero
}
之后,每個重復使用的單元格將自動返回其初始位置。 另外,當您將表格視圖滾動回到頂部時,第一個UICollectionView
也將位於初始位置。
在tableview單元類中,添加collectionview委托和數據源的擴展,定義布局並配置collectionview單元。
在視圖控制器類中,轉到cellForRowAIndex並調用tableview單元格類的方法,例如configureCollectionView(at section:indexPath.row)//捕獲行索引
確保在tableview類中定義了configure方法,該方法設置collectionview委托,數據源並重新加載數據
這樣做可以使每行的滾動位置以似乎沒有鏈接的方式滾動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.