2015-02-06 5 views
1

컬렉션보기, 배열 및 사전에 대한 내 머리를 얻으려고합니다. 나는 배열에서 이미지를 채울 UIImageView 콘센트가 들어있는 CollectionViewCell이라는 클래스가 있습니다. 문제는 다른 콘텐츠가있는 여러 섹션이 있으므로 이미지를 저장하는 여러 개의 배열을 만들었습니다. 현재 코드에서 배열 인덱스가 범위를 벗어났다는 오류가 발생합니다. 정보를 키와 값으로 구분하는 사전으로 다른 섹션을 채워야합니까?컬렉션보기, 배열 및 사전

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell 

    // Configure the cell 

    cell.imageView.image = green[indexPath.row] 
    cell.imageView.image = blue[indexPath.row] 

    return cell 

어떻게 배열에서 채워지는 헤더와 다른 섹션의 이름을 지정할 수 있습니다? names는 문자열 배열이고 HeaderView는 빈 레이블을 포함하는 클래스입니다. 이 코드를 사용하면 오류가 발생합니다.

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 

    switch kind { 
    case UICollectionElementKindSectionHeader: 
     let headerView = 
     collectionView.dequeueReusableSupplementaryViewOfKind(kind, 
      withReuseIdentifier: "HeaderView", 
      forIndexPath: indexPath) 
      as HeaderView 
     headerView.label.text = names[indexPath.row] 
     return headerView 
    default: 
     assert(false, "Unexpected element kind") 
    } 
} 

는 [편집] 섹션 코드 섹션 및 항목 수의 수 :

그래서 녹색 반대로 파란색 섹션의 항목 다른 번호가 있다면
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    //Return the number of sections 
    return 2 
} 


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    //Return the number of items in the section 
    return green.count 
} 

, 나는 수익을 포함 할 numberOfItemsInSection 함수에서도 blue.count를 사용할 수 있습니까? blue이 짧은 배열에 이상이있는 경우, 현재 코드에서

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    //Return the number of items in the section 
    if section == 0 { 
     return green.count 
    } else { 
     return blue.count 
    } 
} 

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell 

    // Configure the cell 

    if indexPath.section == 0 { 
     cell.imageView.image = green[indexPath.row] 
    } else { 
     cell.imageView.image = blue[indexPath.row] 
    } 

    return cell 
} 

: greenblue 이후

+0

녹색과 파란색이 서로 다른 셀을 채우고 있습니까? 또한 collectionView : numberOfItemsInSection : 및 numberOfSectionsInCollectionView : code를 게시 할 수 있습니까? –

+0

예! 녹색과 파란색은 배열에 다른 요소가 있습니다. – Beatle

답변

0

가 다른 부분을 채울로되어있다, 예를 하나를 사용해야하거나 다른 조건 섹션에 따라 green 일 경우 numberOfItemsInSectionblue의 색인을 초과하므로 설명 된 오류가 발생할 수 있습니다.

0

indexPath 개체는 행뿐만 아니라 섹션에 대한 정보도 캡슐화합니다. 따라서 첫 번째 섹션의 첫 번째 요소를 참조하는 경우 indexPath.row은 0이고 indexPath.section은 0이됩니다. 두 번째 섹션의 세 번째 요소를 참조하는 경우 indexPath.row은 2가되고 indexPath.section은 1이됩니다. . 등등. 이 문맥에서는 cellForRowAtIndexPathnumberOfItems 메서드를 어떻게 정의하겠습니까? 코드는 다음과 같습니다.

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    //Return the number of items in the section 
    if(section == 0) { 
     return green.count 
    } else if(section == 1) { 
     return red.count 
    } 
} 

tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!) { 
    //Reusable cell code 
    if(indexPath.section == 0) { 
      //green cell population 
    } 
    else if(indexPath.section == 1) { 
      //red cell population 
    } 
}