Tôi quan tâm đến việc có một bộ sưu tập xem như một phần của ô xem bộ sưu tập nhưng vì một số lý do không thể tìm ra cách thực hiện điều này. Tôi sẽ triển khai các phương pháp cần thiết cho bộ sưu tập ô ở đâu?UICollectionView trong UICollectionViewCell
Trả lời
Có an article that Ash Furrow wrote giải thích cách đặt UICollectionView
bên trong một UITableViewCell
. Về cơ bản nó cũng giống như ý tưởng khi sử dụng nó bên trong một UICollectionViewCell
.
Điều này đã quá muộn cho câu trả lời này nhưng nó có thể giúp người khác. Đây là ví dụ về số UICollectionView
bên trong số UICollectionViewCell
.
Cho phép bắt đầu bằng cách có mainCollectionView
. Sau đó, trên mỗi tế bào của bộ sưu tập này tạo và khởi tạo một nơi mới UICollectionView
và quyền làm điều đó là trong này đại biểu sau đây của UICollectionView
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)
ví dụ như tôi khởi tạo MainCollectionViewCell
đây và sau đó MainCollectionViewCell
xử lý logic để tạo ra một mới UICollectionView
guard let collectionViewCell = cell as? MainCollectionViewCell else { return }
collectionViewCell.delegate = self
let dataProvider = ChildCollectionViewDataSource()
dataProvider.data = data[indexPath.row] as NSArray
let delegate = ChildCollectionViewDelegate()
collectionViewCell.initializeCollectionViewWithDataSource(dataProvider, delegate: delegate, forRow: indexPath.row)
collectionViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
đây là khởi tạo trên MainCollectionViewCell
tạo ra một mới UICollectionView
func initializeCollectionViewWithDataSource<D: protocol<UICollectionViewDataSource>,E: protocol<UICollectionViewDelegate>>(dataSource: D, delegate :E, forRow row: Int) {
self.collectionViewDataSource = dataSource
self.collectionViewDelegate = delegate
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .Horizontal
let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: flowLayout)
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseChildCollectionViewCellIdentifier)
collectionView.backgroundColor = UIColor.whiteColor()
collectionView.dataSource = self.collectionViewDataSource
collectionView.delegate = self.collectionViewDelegate
collectionView.tag = row
self.addSubview(collectionView)
self.collectionView = collectionView
collectionView.reloadData()
}
Hy vọng điều đó sẽ hữu ích !!
Tôi đã làm ví dụ cho điều này và đưa vào github. Nó thể hiện việc sử dụng UICollectionView
bên trong một UICollectionViewCell
.
https://github.com/irfanlone/Collection-View-in-a-collection-view-cell
Mọi thứ đều được thực hiện theo chương trình. Không có bảng phân cảnh.
Tôi đã thêm UICollectionView bên trong UICollectionViewCell của mình. Tôi cũng cho thấy làm thế nào để thêm một lần nữa một UICollectionViewCell bên trong UICollectionView tạo ra để có kết quả này
import UIKit
class CategoryCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
private let cellId = "cell"
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let appsCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
return collectionView
}()
func setupViews() {
backgroundColor = .blue
addSubview(appsCollectionView)
appsCollectionView.delegate = self
appsCollectionView.dataSource = self
appsCollectionView.register(AppCell.self, forCellWithReuseIdentifier: cellId)
addConstrainstWithFormat("H:|-8-[v0]-8-|", views: appsCollectionView)
addConstrainstWithFormat("V:|[v0]|", views: appsCollectionView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
return cell
}
}
class AppCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews(){
backgroundColor = .red
}
}
UICollectionViewController My
import UIKit
class FeaturedAppsController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let cellId = "cell"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
collectionView?.backgroundColor = .white
collectionView?.register(CategoryCell.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(view.frame.width, 150)
}
}
Toàn bộ lời giải thích có thể được tìm thấy và được phát triển bởi "Hãy ´s xây dựng ứng dụng đó ": https://www.youtube.com/watch?v=Ko9oNhlTwH0&list=PL0dzCUj1L5JEXct3-OV6itP7Kz3tRDmma
- 1. UICollectionView không chứa UICollectionViewCell trong IB
- 2. Thêm UICollectionViewCell vào UICollectionView hiện tại
- 3. Ngăn hiển thị hoạt ảnh UICollectionViewCell khi trình bày UICollectionView
- 4. Tạo UICollectionViewCell lớp con với xib
- 5. Cách phóng to UIScrollView bên trong UICollectionViewCell?
- 6. Tạo hiệu ứng UICollectionViewCell trên Tap
- 7. UICollectionView cellForItemAtIndexPath không đăng ký ô
- 8. Hoạt hình tất cả UICollectionViewCells trong UICollectionView
- 9. Chọn các mục lập trình trong UICollectionView
- 10. Đạt được nút bấm trong UICollectionView
- 11. Không thể chọn khi UICollectionView được lồng trong UITableVIew
- 12. Xử lý Nhấn Cử chỉ trong UICollectionView
- 13. UICollectionViewCell Border/Shadow
- 14. UICollectionViewCell lớp con init không bao giờ chạy
- 15. Dừng cuộn trong UICollectionView
- 16. phóng to toàn bộ UICollectionView
- 17. UICollectionView registerCell - Các ô trống
- 18. Thiếu thuộc tính chỉnh sửa và thiết lậpChỉnh sửa: hoạt ảnh: trong UICollectionViewCell
- 19. Cách tìm tọa độ của UICollectionViewCell có liên quan đến Cửa sổ ứng dụng
- 20. subclassing UICollectionViewLayout và gán cho UICollectionView
- 21. Đặt đường viền trong UICollectionView
- 22. iOS Subclass UICollectionViewCell phương thức init tùy chỉnh
- 23. UIButton subview trong UICollectionViewCell không thay đổi trạng thái điều khiển
- 24. Nội dung cũ của UICollectionViewCell được sử dụng lại được hiển thị ngắn gọn khi sử dụng lại các ô trong UICollectionView
- 25. Cách chọn một số mục trong UICollectionView sau lần tải đầu tiên?
- 26. Kỹ thuật tải lười cho UICollectionView
- 27. iOS 6.0: UICollectionView không tôn trọng clipsToBounds với pagingEnabled
- 28. Xóa các ô khỏi UICollectionView qua NSNotification
- 29. UILabel trong UICollectionViewCell tùy chỉnh luôn rỗng, không thể cập nhật văn bản
- 30. Ô UICollectionView biến mất