【问题标题】:Update UILabel in UICollectionViewCell outside collectionView:cellForItemAtIndexPath在 collectionView:cellForItemAtIndexPath 之外的 UICollectionViewCell 中更新 UILabel
【发布时间】:2013-10-23 09:33:37
【问题描述】:

我在 IB 中定义的 ViewController 中的 UICollectionView 中有一个自定义 UICollectionViewCell。在单元格中有一个标签,我可以在 cellForItemAtIndexPath 中设置和修改文本属性。我想要实现的是更改 UILabel 中选定单元格的文本属性。当在 didSelectItemAtIndexPath 中选择单元格时,我存储 indexPath。我想通过单击按钮(集合视图外的按钮)进行更新,并将 UILabel 的文本属性设置为按钮标题(pressedNumber)。

我试过这个:

-(BOOL) calculate: (NSString *) pressedNumber{

talCell *cell = [self.collectionView
                 dequeueReusableCellWithReuseIdentifier:@"cell"
                 forIndexPath:selectedCell];


[cell.cellTitle setText:pressedNumber];
[self.collectionView reloadItemsAtIndexPaths:@[selectedCell]];

调试时我可以看到 cell.cellTitle.text 属性与pressedNumber 相同。 感谢任何可以引导我找到正确代码的建议。

【问题讨论】:

    标签: ios uilabel uicollectionviewcell


    【解决方案1】:

    如果您有单元格的 indexPath,请使用 cellForItemAtIndexPath 获取它

    talCell *cell = [self.collectionView
                     cellForItemAtIndexPath:indexPath];
    
    
    [cell.cellTitle setText:pressedNumber];
    

    【讨论】:

    • 我试过这个 talCell *cell = [self.collectionView cellForItemAtIndexPath:selectedCell];但是使用“UICollectionViewCell *”类型的表达式初始化“talCell *__strong”得到不兼容的指针类型
    • 如果您使用自定义单元格,您可能需要强制转换:talCell *cell = (talCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多