你知道在tableviews
你可以设置一个数组,而不是把数组的每个元素都作为row
的文本。例如:
我有tableView
。
array : items = ["hello","world","yes"]
在cellForRowAtIndexPath
函数中,您只需说:cell.text = items[indexPath.row]...
这是一个简短的例子。
所以现在是问题。我有一个集合视图,它有两列(numberOfItemsInSection
函数返回此项),我有33行({{1}}函数返回此项)....意味着我有numberOfSectionsInCollectionView
个单元格。我想让每个单元格显示其中的另一个文本,所以我创建了一个包含66个元素的字符串“项目”。从位置0到位置65 ......我该怎么办?就像在tableview的例子中一样,只写66(2x33)
这是我的问题。我不知道如何识别每个单元格,彼此不同。
我会指定如果我写:cell.text = items[I DON'T KNOW WHAT TO WRITE HERE]...
它将在第一列中放入数组的第一项,在第二列中放入数组的第二项,那就是全部。我也尝试使用cell.text = items[indexPath.row]
,但它也像indexPath.item
一样。
谢谢你的帮助!这对我有很大的帮助!
答案 0 :(得分:0)
只需将您的项目整理到两个数组中,
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
}