2017-10-13 54 views
0

我想弄清楚如何捕獲我的集合視圖的當前索引路徑。UICollectionView - 找出當前索引路徑並更改值/數據

我使用的水平流動,我已經指出以下幾點:

func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 3 
    } 

有三個預定義的細胞,以及我想更改每個三者的文本/ UI元素相應地收集視圖單元格。我怎樣才能做到這一點?我真的很掙扎,因爲目前我正在獲得3個充滿相同數據的單元。這是預期的。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

       let cell = detailsCollectionView.dequeueReusableCell(withReuseIdentifier: "DetailsCollectionViewCell", for: indexPath) as! DetailsCollectionViewCell 
       cell.layer.shadowRadius = 1 
       cell.layer.shadowOpacity = 0.1 
       cell.layer.shadowOffset = CGSize(width: 2.5, height: 5) 

       return cell 
     } 
    } 
} 

但在本質上我希望做到以下幾點,僞代碼:

if cell.indexPath == 0 { 
     label.text = "This is the first cell" 

    } else if cell.indexPath == 1 { 
    label.text = "This is the second cell" 

    } else if cell.indexPath == 2 { 
    label.text = "This is the third cell" 
    } 
} 

道歉,如果這是一個愚蠢的問題。

謝謝你的時間。

+0

在你的僞代碼,它寫在'的CollectionView(_的CollectionView:cellForItemAt:)'和'替換如果cell.indexPath == 0'和'indexPath.row == 0'(依此類推)。 – Larme

+0

@Larme在'UICollectionView'中,您應該使用'item'而不是'row'來找出一個項目的編號。 – the4kman

+0

我很掙扎,我在'collectionView(_ collectionView:cellForItemAt:)'之前調用了什麼?愚蠢的問題,我知道。 – SwiftLearner

回答

0

您應該比較cellForItemAt的參數indexPathitem屬性。

if indexPath.item == 0 { 
    //first item 
} 
+0

非常感謝。我會接受答案,但低代表,悲傷。但是,再一次,非常感謝你的親切先生!編輯 - 我必須等一分鐘。然後我會接受。再次感謝你。 – SwiftLearner

+0

@SwiftLearner歡迎您。你可以在一分鐘左右接受這個答案,這是一個時間問題,而不是缺乏聲譽。 – the4kman

+0

是的,我想哈哈!再次感謝你。我已經接受了你的回答。 – SwiftLearner

相關問題