繁体   English   中英

iOS Swift UICollectionView,我想要每个单元格都有不同的标签吗?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM