【发布时间】:2023-03-23 13:22:01
【问题描述】:
所以我有一个视图控制器,其集合视图包含 numberOfItemsInSection 中的两个集合视图单元格。每个单元格中都有一个表格视图。单击某个选项卡栏项目时,我试图转到表格视图的顶部。我的问题是我无法访问我的集合视图单元格中的标签栏,当我访问我的视图控制器中的标签栏并从视图控制器调用我的集合视图单元格中的一个函数时它不起作用(它说我我的表格视图中没有任何行)。
这是我在视图控制器中设置集合视图,并在视图控制器中调用 func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) 时尝试将集合视图单元格的表格视图发送到顶部。
class AllPostsVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITabBarControllerDelegate {
var indexPath:IndexPath?
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
self.tabBarController?.delegate = self
}
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.dataSource = self
cv.delegate = self
return cv
}()
func setupCollectionView() {
view.addSubview(collectionView)
view.bringSubviewToFront(collectionView)
collectionView.isPagingEnabled = true
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.heightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.heightAnchor).isActive = true
collectionView.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor).isActive = true
collectionView.topAnchor.constraint(equalTo: self.menuBar.bottomAnchor).isActive = true
collectionView.register(AllPostsCell.self, forCellWithReuseIdentifier: cellId)
collectionView.register(AllPostsUnderReviewCell.self, forCellWithReuseIdentifier: postsUnderReviewcell)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
for cell in collectionView.visibleCells {
indexPath = collectionView.indexPath(for: cell)
print(indexPath)
}
}
func scrollToMenuIndex(menuIndex: Int) {
let indexPath = NSIndexPath(item: menuIndex, section: 0)
collectionView.scrollToItem(at: indexPath as IndexPath, at: [], animated: true)
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if tabBarIndex == 0 {
if indexPath?.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath!) as! AllPostsCell
cell.sendToTop()
}
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var albumsCell:UICollectionViewCell?
if indexPath.item == 1 {
let underReviewCell = collectionView.dequeueReusableCell(withReuseIdentifier: postsUnderReviewcell, for: indexPath) as! AllPostsUnderReviewCell
underReviewCell.parent = self
return underReviewCell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! AllPostsCell
cell.parent = self
return cell
}
在我的收藏视图单元格中
class AllPostsCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
func sendToTop() {
DispatchQueue.main.async {
self.layoutIfNeeded()
self.myTableView.layoutIfNeeded()
let indexPath = NSIndexPath(row: 0, section: 0)
self.myTableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)
}
}
}
当我这样做时,我得到了错误:
Terminating app due to uncaught exception 'NSRangeException', reason: 'Attempted to scroll the table view to an out-of-bounds row (0) when there are only 0 rows in section 0. 即使我在表格视图中看到行。
虽然它确实再次调用了我的集合视图单元格的 init 方法。
那么当我点击某个标签栏项目时,将我的 tableview 发送到我的集合视图单元格中的顶部有什么好的解决方案?
【问题讨论】:
标签: swift uiviewcontroller uicollectionview uicollectionviewcell uitabbaritem