![](/img/trans.png)
[英]When I want to add a cell in UICollectionview it appears two - Swift 3
[英]IOS Swift UICollectionView,I want a different tag for every cell?
您知道在表tableviews
可以設置一個數組,然后將數組的每個元素都作為該row
的文本,例如:
我有一個tableView
。
array : items = ["hello","world","yes"]
在cellForRowAtIndexPath
函數中,您只是說: cell.text = items[indexPath.row]...
這是一個簡短的示例。
所以現在是問題了。我有一個集合視圖,它有兩列( numberOfItemsInSection
函數返回這個),我有33行( numberOfSectionsInCollectionView
函數返回這個)....這意味着我有66(2x33)
格。為了讓每個單元格在其中顯示另一個文本,所以我創建了一個由66個元素組成的String數組“ items”。從位置0到位置65 ...如何像表格視圖中的示例一樣,只需編寫cell.text = items[I DON'T KNOW WHAT TO WRITE HERE]...
這是我的問題。我不知道如何識別每個單元格,以使其彼此不同。 我要指定的是,如果我要寫: cell.text = items[indexPath.row]
,它將在第一列中放置數組的第一項,在第二列中放置數組的第二項,僅此而已。 我也試圖與indexPath.item
但它確實喜歡同樣indexPath.row
。
謝謝你的幫助! 這對我有很大幫助!
只需將您的物品整理成兩個陣列,
override func viewDidLoad() {
super.viewDidLoad()
self.items1 = ["hello", "word"]
self.items2 = ["hello2", "word2"]
self.itemsSections = [items1, items2]
}
我附上了UITableView的示例,但您可以輕松地將其應用於UICollectionView數據源協議,然后在表數據源中使用itemsSections:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return itemsSections.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let section = itemsSections[section]
return section.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let section = itemsSections[indexPath.section]
cell.text = section[indexPath.row]
return cell
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.