![](/img/trans.png)
[英]Proper way to reuse UITableViewCell containing UICollectionView
[英]UICollectionView reuse in UITableViewCell
我知道這個問題已經問了很多次,但是沒有解決方案可行。
我在uitableview單元格中有集合視圖,我在故事板中准備了一個表視圖和集合視圖的單元格,我的問題是假設我滾動單元格1集合視圖,然后單元格4集合也自動滾動到該位置。 我怎么能處理這種情況..
請注意我現在制作一個靜態的屏幕設計
我的代碼
// MARK:
// MARK: table view delegate method
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if Appconstant.isIpad()
{
return 215.0
}
return 185.0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let strCell = "WorkoutCell" //+ "\(indexPath.row)"
let cell = tableView.dequeueReusableCellWithIdentifier(strCell) as? WorkoutCell
// if cell == nil
// {
// cell = work
// }
return cell!
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let cell2 = cell as! WorkoutCell
cell2.collecionWorkout.reloadData()
}
// MARK:
// MARK: collectionview delegate and data source
//1
func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
//2
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: NSIndexPath) -> CGSize {
// Adjust cell size for orientation
if Appconstant.isIpad() {
return CGSize(width: 160, height: 150)
}
return CGSize(width: 140.0, height: 130.0)
}
//3
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("WorkoutCollectionCell", forIndexPath: indexPath)
// Configure the cell
return cell
}
uitableview單元格代碼
class WorkoutCell: UITableViewCell {
@IBOutlet weak var collecionWorkout: UICollectionView!
var flowLayout : UICollectionViewFlowLayout!
override func awakeFromNib() {
super.awakeFromNib()
self.flowLayout = UICollectionViewFlowLayout()
if Appconstant.isIpad()
{
self.flowLayout.itemSize = CGSize(width: 160, height: 150)
}
else
{
self.flowLayout.itemSize = CGSize(width: 140, height: 130)
}
self.flowLayout.scrollDirection = .Horizontal
self.flowLayout.minimumInteritemSpacing = 10.0
flowLayout.sectionInset = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0);
collecionWorkout.collectionViewLayout = flowLayout
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我滾動的Cell 1st
單元格4自動滾動
有兩種情況。
1 - 重置UICollectionView偏移位置:
要重置collectionview位置,只需在cellForRowAtIndexPath中調用cell.collectionView.contentOffset = .zero
2 - 保持上一個滾動位置:
要保持上一個滾動位置,您需要在包含視圖控制器中的數據模型旁邊存儲每個單元格的偏移列表。 然后,您可以在didEndDisplayingCell
保存給定單元格的偏移量,並將其設置為cellForRowAtIndexPath
而不是.zero
。
UITableView
致力於reusability
的概念。 每當滾動tableView
時, tableView
中的cells
tableView
定會被重用。
現在出現了2個場景,
獲取一個新單元格 - 當重復使用tableViewCell
,其中的collectionView
也將被重用,因此這就是保留前一個單元格的contentOffset
的原因。
Scroll to previous cell
- 如果我們手動滾動到collectionView
的特定cell
,我們可能希望在滾動回到它時保留其位置。
要在tableView
獲得這種功能,我們需要 - 為1st case
手動重置contentOffset
,並且需要在第二種1st case
保留contentOffset
。
您可以遵循的另一種方法是使用combination of UIScrollview and UIStackView
的combination of UIScrollview and UIStackView
。
這是怎么回事。
在storyboard
,創建一個UIScrollView。 添加一個垂直的UIStackView。
在UICollectionViews
中添加4個stackView
。 此編號可根據您的要求進行配置。
將controller
添加為所有collectionViews
的dataSource
和delegate
。
即
class ViewController: UIViewController, UICollectionViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
cell.label.text = "\(indexPath.row)"
return cell
}
}
class CustomCell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
}
現在,由於我們使用的是stackView
,因此不會重復使用任何collectionViews
。 因此,所有collectionViews
的contentOffsets
將保持不變。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.