ios - UICollectionViewCell 中的 UIButton 文本被覆盖。我究竟做错了什么?

标签 ios objective-c uibutton uicollectionviewcell

我有一个 UICollectionView,单元格从 UICollectionViewCell 子类化,并在子类的初始化中从 nib 加载。子类单元格包含 UIButton 的子类,其中文本在界面构建器中设置为 TXT。
出于测试目的,我做了:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ISOCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath];

    cell.uicellbtn.titleLabel.text = [NSString stringWithFormat:@"%d", (int)indexPath.row];

    return cell;
}

并且可以看到数字序列(1-9)显示在单元格中,但仅显示一秒钟,直到它们被界面构建器(TXT)中的默认文本替换!

最佳答案

要设置按钮文本,您必须使用 setTitle:forState:方法。

[cell.uicellbtn setTitle:[NSString stringWithFormat@"%d", (int)indexPath.row] forState:UIControlStateNormal];

这仍然时不时地让我绊倒。

关于ios - UICollectionViewCell 中的 UIButton 文本被覆盖。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32236615/

相关文章:

ios - 如何显示 Json 对象中的键和值

ios:添加注释并调用 mapView.showAnnotations() 后禁用 mapView 更新

ios - 以编程方式调用 PerformSegueWithIdentifier 不会执行任何操作

ios - Objective C - TableView 无法在屏幕上显示数据

ios - 如果 UITextField 和 UITextView 在 Swift 中为空,则禁用 UIButton

iphone - 编写父 View Controller 的终极指南

附加到正在运行的进程时 iPhone GDB 段错误

ios - 将方法添加到类别(来自 github)

ios - 使用 UiButton 反复更改 UILabel 文本

swift - 如何将相机或图库拍摄的图像添加到按钮?