gpt4 book ai didi

ios - 如何获取单元格中标签的值

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:17:27 24 4
gpt4 key购买 nike

我使用了 UICollectionView 并使用单元格放置了一个与我的值相关联的标签。当您插入单元格时,我想将文本带到所选标签并用它来做任何其他事情。我能怎么做?

使用这段代码,我可以准确地返回到索引,但我不能将单元格的内容放入字符串中。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"Cell";
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

NSString *strColorCell = cell.lblCustomCell.text;
NSLog(@"Indice della cella: %i %i %@", indexPath.item, indexPath.section * 2 + indexPath.row, strColorCell);
}

最佳答案

这一行

CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

给您已选择的项目,它给您一个未使用的单元格,该单元格已被回收。你需要打电话

CustomCell *cell = [collectionView.dataSource collectionView:collectionView cellForItemAtIndexPath:indexPath];

获取正确的单元格。

但是,从根本上说,这是一种错误的方法:您应该使用 indexPath 来查询模型中的标签文本,而不是询问 View 。查看您的collectionView:cellForItemAtIndexPath 方法的代码,找到您使用indexPath 设置标签文本的地方。文本来自您的模型类。您应该在 didSelectItemAtIndexPath 方法中做同样的事情。

关于ios - 如何获取单元格中标签的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19269686/

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