2016-10-05 9 views
0

私はcarの配列から2番目と3番目の値を取得しようとしています。私は 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配列の2つの項目を持つ必要があります。セルタイトルは= "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 
} 
関連する問題