[英]Collection Views, Arrays, and Dictionaries
試圖讓我了解集合視圖,數組和字典。 我有一個名為CollectionViewCell的類,其中包含一個UIImageView出口,我希望填充數組中的圖像。 麻煩的是我有不同的部分具有不同的內容,所以我創建了多個存儲圖像的數組。 使用當前代碼,我得到一個錯誤,指出數組索引超出范圍。 我是否需要用字典填充不同的部分,而該字典用鍵和值來分隔信息?
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
如何使用從數組填充的標題來命名不同的部分? 名稱是我的字符串數組,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函數中也包括return blue.count?
由於green
和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
}
在當前代碼中,如果blue
是比green
短的數組,則由於numberOfItemsInSection
超出了blue
的索引,您將得到描述的錯誤。
indexPath
對象不僅封裝有關行的信息,而且還封裝有關節的信息。 因此,如果您引用第一部分中的第一個元素,則indexPath.row
將為0, indexPath.section
將為0。如果您引用第二部分中的第三個元素, indexPath.row
將為2,並且indexPath.section
將為1 ...依此類推。 在這種情況下,您將如何定義cellForRowAtIndexPath
和numberOfItems
方法? 這是代碼:
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.