gpt4 book ai didi

IOS Swift UICollectionView,我想为每个单元格设置不同的标签?

转载 作者:行者123 更新时间:2023-11-28 09:16:19 25 4
gpt4 key购买 nike

您知道在 tableviews 中您可以设置一个数组,然后将数组的每个元素作为 的文本,例如:

我有一个tableView

数组:items = ["hello","world","yes"]

cellForRowAtIndexPath 函数中,您只需说:cell.text = items[indexPath.row]...这是一个简短的示例。

现在是问题所在。我有一个 Collection View ,它有两列(numberOfItemsInSection 函数返回这个),我有 33 行(numberOfSectionsInCollectionView 函数返回这个)...这意味着我有 66(2x33) 单元格。我希望每个单元格在其中显示另一个文本,所以我制作了一个包含 66 个元素的字符串“项目”数组。从位置 0 到位置 65...我怎么能像在 tableview 的例子中那样,只写 cell.text = items[我不知道在这里写什么]...这是我的问题。我不知道如何识别每个单元格,使其彼此不同。我会指定如果我写: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 数据源协议(protocol)然后在表数据源中使用 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
}

关于IOS Swift UICollectionView,我想为每个单元格设置不同的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27452232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com