2016-10-05 2 views
0

저는 car 만들기 배열에서 두 번째와 세 번째 값을 가져오고 아래 함수에서 반환하려고합니다. 나는 cell.title.text = carmake[1...2][indexPath.row]cell.title.text = carmake[indexPath.[1...2]]을 시도했지만 그 중 누구도 맞지 않습니다. 당신이 그들을 결합하려는 경우 시도 할 수 있습니다, 나에게indexpath.row의 배열에서 특정 값을 얻습니다.

carmake = ["hi","hello","how are you","i am good"] 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = viewcontrol.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! jobtypeCollectionViewCell 

    cell.title.text = carmake[indexPath.row] 
    return cell 
} 
+0

귀하의 질문에 (적어도 나에게) 명확하지 않다. 수집보기가있는 항목의 수와 각 항목의 제목은 무엇입니까? –

+0

콜렉션 뷰에는 carmake 배열의 항목이 두 개 있어야하지만 셀 제목은 "hello", "how are you"여야합니다. carmake 배열에 4 가지 값이 있지만 4 가지 중에서 2 가지 값만 가져 오려고합니다. – jakson

답변

0

도와주세요 :

cell.title.text = carmake[1] + carmake[2] 
0

당신이 collectionview에 배열에서 단지 두 번째와 세 번째 항목을 반환 할 경우, 이것은 무엇을 당신 시도해야합니다 :

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    //returns number of sections in collectionview, which in your case seems to be 1 
    return 1 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    //returns number of items per section, which in your case, you have stated is 1 
    return 1 

} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = viewcontrol.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! jobtypeCollectionViewCell 
    //indexpath.item starts at 0. indexPath.item + 1 and + 2 because you want to return carMake[1] and carMake[2]. 
    cell.title.text = carmake[indexPath.item + 1] + carmake[indexPath.item + 2] 

    return cell 
} 
관련 문제