我有一个 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/