Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UILabel隐藏文本问题_Ios_Objective C_Iphone_Xcode_Uicollectionview - Fatal编程技术网

Ios UILabel隐藏文本问题

Ios UILabel隐藏文本问题,ios,objective-c,iphone,xcode,uicollectionview,Ios,Objective C,Iphone,Xcode,Uicollectionview,这太奇怪了。我也不太愿意发表这个问题,但我没有任何解决办法,这就是为什么我决定发表它。我来了 我有一个XIB,我在UICollectionView中对它进行了子类化。这是我在didSelectMethod中的代码: - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { CollectionViewCell *cell = (Co

这太奇怪了。我也不太愿意发表这个问题,但我没有任何解决办法,这就是为什么我决定发表它。我来了

我有一个XIB,我在UICollectionView中对它进行了子类化。这是我在didSelectMethod中的代码:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

 CollectionViewCell *cell = (CollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];

cell.nameLabel.textColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.8];

[cell setSelected:NO];

cell.nameLabel.hidden = NO;
cell.nameLabel.text = @"HHHHHHH";
cell.nameLabel.backgroundColor = [UIColor redColor];

}
现在,当我运行代码时,我得到了正确的输出。但是当我选择任何
单元格
时,我的
ui标签
的文本被隐藏,我只能看到
背景
颜色

当我选择另一个标签时,我会返回以前选择的
UILabel
的文本

我还没有写过任何其他我无法理解的代码

当我选择任何单元格时:

当我选择另一个单元格时:

选择上一个单元格时:

以下是我制作电池的代码:

 - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {

// Get reusable cell reference
CollectionViewCell *cell = (CollectionViewCell *)[cv dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];



[cell updateText];




return cell;
}
此方法位于自定义类中

 - (void) updateText{


self.nameLabel.text = name;

 }

它很高可能是因为您的
CollectionViewCell
是可重用的单元。您的
hidden
状态也会重新用于其他单元格。您应该在
cellForIndexPath
方法中设置它

- (void)updateText
{
    self.nameLabel.text = name;
    cell.nameLabel.hidden = YES; // set here it's default value
}

didSelectItemAtIndexPath
中捕获所选索引,然后重新加载集合视图。在
cellForIndexPath
中检查所选索引并在那里执行操作。做下面这样的事情

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (selectedIndex == indexPath.row) {
        cell.nameLabel.textColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.8];

        [cell setSelected:NO];

        cell.nameLabel.hidden = NO;
        cell.nameLabel.text = @"HHHHHHH";
        cell.nameLabel.backgroundColor = [UIColor redColor];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
       selectedIndex = indexPath.row;
       [collectionView reloadData];
}

为每个单元格创建布尔数组,然后在选择任何单元格时更新值,即在
didSelectItemAtIndexPath
中。
同样在
cellForItemAtIndexPath
中,你可以按照Vijay的话去做,除了只考虑一个选定的单元格,你现在可以处理多个选定的单元格。

真正的问题是你应该在不知不觉中将标签的突出显示颜色设置为清除xib中的颜色。检查笔尖并将突出显示的属性更改为所需的颜色。选择单元后,子视图将其颜色更改为其在xib中设置的高亮显示颜色。希望这对你有帮助

转到nib/xib文件,检查高亮显示的颜色是否清晰或其他颜色。如果很清楚,可以根据您的要求进行更改


您是否实现了
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
?显示您的实现
UICollectionView
将重用以前使用的单元格,因此如果您将标签设置为隐藏,那么它将对所有其他单元格隐藏。您应该创建一个数组来存储每个单元格的选定状态,然后在
cellforrowatinexpath
中,基于该数组设置标签隐藏。@bamswold Edited。谢谢,我也面临同样的问题,只需在didSelectItemAtIndexpath中写入重载数据就可以解决问题,但我不想在每个选择上都重新加载数据。我在故事板中控制它,而不是手动创建它,是否有其他方法可以克服这个问题。请帮忙