2016-12-21 13 views
9

Trong dự án của tôi, tôi đã sử dụng một tableView bên trong một collectionView và tôi cũng sử dụng một pageControl. Khi tôi vuốt chế độ xem bộ sưu tập theo chiều ngang, tôi cũng muốn chuyển đổi pageControl, nhưng nó không hoạt động đúng cách. Tôi đã sử dụng phương pháp scrollViewDidScroll để chuyển đổi pageControl thành số collectionView Swipes. Nhưng vấn đề là, khi tableView cuộn scrollViewDidScroll phương pháp một lần nữa sẽ gọi. Vì vậy, có cách nào khác hoặc bất kỳ giải pháp nào để khắc phục sự cố này.Cách sử dụng trangControl trong Collectionview Bên trong Tableview?

Kiểm tra liên kết bên dưới.

enter link description here

TableView Phương pháp

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    return model.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FbHomeCell 
    cell.btnPushImage.tag = indexPath.row 
    cell.collectionView.tag = indexPath.row 
    return cell 
} 

CollectionView Phương pháp

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
{ 
    return model[collectionView.tag].count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{ 
     collCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FBHomeCollectionCell  
     collCell.backgroundColor = model[collectionView.tag][indexPath.item]  
     collCell.pageControlSwipe.numberOfPages = Int(collectionView.contentSize.width/collectionView.frame.size.width)      
     return collCell 
} 

Scroll Xem Đại biểu Phương pháp

func scrollViewDidScroll(_ scrollView: UIScrollView) 
{ 
    let pageWidth = scrollView.frame.width 
    collCell.pageControlSwipe.currentPage = Int((scrollView.contentOffset.x + pageWidth/2)/pageWidth) 

    collCell = cell.collectionView.cellForItem(at: IndexPath (item: scrollView.tag, section: 0)) as! FBHomeCollectionCell 

} 

Cảm ơn và đánh giá cao sự giúp đỡ ..

Trả lời

11

Bạn có thể so sánh mà xem được di chuyển hiện đang nằm trong scrollViewDidScroll.

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    if scrollView == self.collectionView { //Your scrollView outlet 
     //Your code here 
    } 
} 

Edit: Trước hết bạn cần phải thực hiện nguồn dữ liệu và phương pháp đại biểu của collectionView với tableViewCell của bạn không phải với ViewController bạn cũng có thể bạn cần phải đặt pageControlSwipe trong tableViewCell không có trong collectionViewCell. Vì vậy, nó phải giống như thế này.

class FbHomeCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate { 

    @IBOutlet var collectionView: UICollectionView! 
    @IBOutlet var pageControlSwipe: UIPageControl! 
    @IBOutlet var btnPushImage: UIButton! 

    var colorArray = [UIColor]()    
    var currentPage = 0 

    func setCollectionViewWith(colorArray: [UIColor]) { 
     self.colorArray = colorArray 
     self.collectionView.isPagingEnabled = true 
     self.collectionView.datasource = self 
     self.collectionView.delegate = self 
     self.collectionView.reloadData() 
    }   

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    { 
     return colorArray.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    { 
     let collCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FBHomeCollectionCell 
     collCell.backgroundColor = self.colorArray[indexPath.item] 
     self.pageControlSwipe.numberOfPages = colorArray.count 
     return collCell 
    } 

    //ScrollView delegate method 
    func scrollViewDidScroll(_ scrollView: UIScrollView) 
    { 
     let pageWidth = scrollView.frame.width 
     self.currentPage = Int((scrollView.contentOffset.x + pageWidth/2)/pageWidth) 
     self.pageControlSwipe.currentPage = self.currentPage 
    } 
} 

Bây giờ gọi phương thức này trong phương pháp nguồn dữ liệu của bảngView như thế này.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return model.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FbHomeCell 
     cell.btnPushImage.tag = indexPath.row 
     cell.collectionView.tag = indexPath.row 
     cell.setCollectionViewWith(colorArray: model[indexPath.row]) 
     return cell 
    } 
+0

có tác phẩm của nó nhưng một lần nữa khi sẽ cuộn bộ sưu tập xem trên hàng tiếp theo của tableview thì lần xem thứ 1 thu thập hiển thị pagecontrol từ 0 chỉ mục. – ashmi123

+0

@ ashmi123 Kích thước ô của bộ sưu tập của bạn có phải là kích thước xem của bạn không, có nghĩa là kích thước thiết bị? –

+0

Không chờ đợi sẽ cho bạn thấy những gì chính xác muốn từ đó u có ý tưởng. – ashmi123

Các vấn đề liên quan