2014-12-12 6 views
1

따라서 UICollectionView 기능을 확장하고 셀 등을 재정의해야합니다. 모든 예제에서 서브 클래스가 UICollectionViewController 인 것을 볼 수 있지만, 단지 UICollectionView이 아닌 것을 볼 수 있습니다.하위 클래스 UICollectionView

스토리 보드의 단일 페이지 (UIViewController)에 대한 수퍼 클래스로 새 클래스 (GridSet)를 만들고 싶지 않습니다. 더 많은 요소 (맞춤 검색 버튼, 라벨 등)가 있기 때문에 유지 관리하고 싶지 않습니다. 그들은 나의 새로운 수업에서,하지만 내 페이지 UIViewController에서 그들을 유지하고 싶습니다. 내가 UICollectionViewUICollectionViewDataSource를 서브 클래스있을 때

나는 단 한 UIViewController 내 서브 클래스의 두 가지를 삽입 할 위치하며 (그러나 다른 UIViewController 하나의 내부 UIViewController를 배치 할 수 없습니다.

을 문제이며, 스토리 보드에서 할 , 그것도 초기화하지 않는 것 같다

class GridSet: UICollectionView, UICollectionViewDataSource { 

let computers = [ 
    ["Name" : "MacBook Air", "Color" : "Silver"], 
    ["Name" : "MacBook Pro", "Color" : "Silver"], 
    ["Name" : "iMac", "Color" : "Silver"], 
    ["Name" : "Mac Mini", "Color" : "Silver"], 
    ["Name" : "Mac Pro", "Color" : "Black"] 
] 

override func awakeFromNib() { 
    super.awakeFromNib() 

    let nib = UINib(nibName: "GridSetCell", bundle: nil) 
    self.registerNib(nib, forCellWithReuseIdentifier: "GridSetCellDefault") 
} 

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

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = self.dequeueReusableCellWithReuseIdentifier("GridSetCellDefault", forIndexPath: indexPath) as GridSetCell 

    cell.label = computers[indexPath.row]["Name"]! 

    return cell 
} 

} 

나는 사용자 정의 셀 펜촉을 사용합니다.

class GridSetCell: UICollectionViewCell { 

@IBOutlet private weak var cLabel: UILabel! 
@IBOutlet private weak var cBack: UIImageView! 

var label: String = "" { 
    didSet { 
     if (label != oldValue) { 
      cLabel.text = label 
     } 
    } 
} 

var back: String = "" { 
    didSet { 
     if (back != oldValue) { 
      cBack.image = UIImage(named: back) 
     } 
    } 
} 

} 

답변

1

UICollectionViewCell과 UICollectionViewLayout을 하위 클래스 화하여 시작하여 시스템 UICollectionView에서 버전을 사용하여 목표를 달성하십시오. 이 자습서에서는 다음 단계를 보여줍니다. http://skeuo.com/uicollectionview-custom-layout-tutorial 이러한 기능이 작동 중일 때 UICollectionView의 하위 클래스를 계속 사용해야하는 경우 새 하위 클래스를 "설치"할 구성 요소가 있어야합니다.

관련 문제