2017-11-19 22 views
1

내보기 컨트롤러에는 6 개의 버튼/세그먼트가있는 UISegmentedControll이 있습니다. 또한 Segmented Controll 아래에 UITableView가 있습니다. 클릭하여 세그먼트를 전환하면 테이블보기가 다른 데이터로 다시로드됩니다. 이제는 사용자가 왼쪽에서 오른쪽으로 또는 오른쪽에서 왼쪽으로 테이블 뷰를 스 와이프하면 뷰 컨트롤러에 페이지 매김과 같은 동작이 표시되는 방식을 구현하려고합니다. 테이블 뷰가 세분화 된 컨트롤과 함께 수평 스크롤됩니다. 가장 좋은 방법으로 앱에서이 기능을 어떻게 구현할 수 있습니까? 내 테이블에는 세 개의 다른 사용자 정의 셀이 있으며 그 중 하나에도 셀 내부에 UICollectionView가 있습니다. Pls 날 도와. 감사.UITegmentedControl과 함께 UITableView로 페이지 매김을 만드는 방법

+0

당신의 UISegmentedControl을 사용하면 더 쉽게 작업 할 수 있습니다. –

답변

3

tableView 안에 collectionView을 사용하는 것이 좋습니다. 수행 할 단계 -

1) segmentedControlcollectionView을 컨트롤러에 추가하십시오.

2) 엘로우 아이콘 컨트롤러 collectionView에서 드래그와 컨트롤 dataSourcedelegate 연결한다.

3) cell 높이를 조정하십시오.

4) collectionView을 선택하고 속성 검사기으로 이동하십시오. 찾으십시오 - : 1) 방향을으로 변경하고 을 가로으로 지정하십시오. 2) Paging Enabled 매개 변수를 확인하십시오.

5) viewsConstraints을 적용하십시오.

6) collectionView 셀 식별자를 지정하십시오.

7) 사용자 지정 collectionView 세포 등급을 지정하십시오.

8) Outlets을 모두 views으로 연결하십시오.

컨트롤러 클래스 - :

import UIKit 

class ViewController: UIViewController { 

    // OUTLETS 
    @IBOutlet weak var controls: UISegmentedControl! 
    @IBOutlet weak var horizontalCollectionView: UICollectionView! 

    // ARRAy OF TYPE UICOLOR 
    var collectionViewColors = [UIColor.gray,UIColor.green,UIColor.red,UIColor.yellow,UIColor.blue,UIColor.brown] 

    // ViewDidLoad 
    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 
    //didReceiveMemoryWarning 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
    // Function to calculate cell index on scroll end. 
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>){ 
     let pagingIndex = targetContentOffset.pointee.x/self.view.frame.width 
     // Set segmented controll current index 
     controls.selectedSegmentIndex = Int(pagingIndex) 
    } 

} 

// Collection view methods 

extension ViewController : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{ 
    // number of section 
    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 
    // number of items in section 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return collectionViewColors.count 
    } 
    // deque cell 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection", for: indexPath) as! HorizontalCell 
     cell.horizonatlColorsView.backgroundColor = collectionViewColors[indexPath.item] 
     return cell 
    } 
    // set minimum line spacing to zero. 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
     return 0 
    } 
} 

되는 사용자 정의 셀 클래스 - :

import UIKit 

    class HorizontalCell: UICollectionViewCell { 
     // UIView outlet 
     @IBOutlet weak var horizonatlColorsView: UIView! 
    } 

모든 것을 설정 한 후, 추가 할 수 want.Also 당신이 출력 당신을 얻을 것이다 가로로 스크롤 tableView 안에 collectionView 지금 있습니다.

출력 - :

enter image description here 난 당신이 수집 뷰 가로로 스크롤하고도 만들 것이다, 그들 매김을 활성화 할 수 있도록이, 콜렉션 뷰 내에서 테이블 뷰해야 가정

enter image description here