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

时间:2014-12-12 21:16:29

标签: ios xcode swift ios8 uicollectionview

你知道在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一样。

谢谢你的帮助!这对我有很大的帮助!

1 个答案:

答案 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
}
相关问题