【问题标题】:Selected UICollectionViewCell not highlighting Image when view reappears on certain cells当视图重新出现在某些单元格上时,选定的 UICollectionViewCell 不突出显示图像
【发布时间】:2014-04-09 16:01:26
【问题描述】:

我有一个带有Tab Bar Controller 的简单应用程序,其中第一个tabUITableViewController(时间线),第二个tab 也是UITableViewController(设置)。

在“设置”中,用户可以单击“主题”cell 并转到UICollectionView,每行 3 个单元格和 4 行。

cells 包含由NSArraysviewDidLoadviewDidLoad 中填充的图像和标签@。

当我选择一个单元格时,我有一个自定义图像出现(在单元格顶部),我注意到在某些情况下,如果我选择一个单元格并转到另一个选项卡(时间轴)并再次返回,所选单元格的自定义图像不会重新出现。

但是,如果我选择一个单元格,然后通过弹出 segue(推送)返回设置,然后返回 UICollectionView,则会出现勾号。

澄清:步骤是:

1) 选择主题

2) 自定义图像(勾号)出现在该单元格上

3) 返回时间轴(不返回设置)

4) 回到第二个标签页,UICollectionView 还在加载中

5) 勾号不存在*

*这仅发生在 12 张图像中的大约 8 张中,但其中 4 张正在工作,这让我感到困惑。

这是cellForItem中的代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ThemeCell *ThemeCell = (ThemeCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Theme Cell" forIndexPath:indexPath];
    NSString *cellData = [self.themeLabels objectAtIndex:indexPath.row];
    UIImageView *dot = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 1, 2)];
    dot.image=[UIImage imageNamed:@"Tick.png"];
    themeCell.cellLabel.text = cellData;
    themeCell.cellImages.image = self.themeImages[indexPath.row];

    self.selectedThemeString = [[NSUserDefaults standardUserDefaults] objectForKey:@"Selection"];

    NSLog(@"Selected Theme String = %@", self.selectedThemeString);

    if (!self.selectedThemeString)
    {        
        self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        themeCell.backgroundView = dot;
        [themeCell addSubview:dot];
    }

    if([self.checkedIndexPath isEqual:indexPath])
    {
        NSLog(@"Loaded");
        themeCell.backgroundView = self.checkmark;
        [themeCell addSubview:self.checkmark];
    }
    else
    {
        NSLog(@"Not Loaded");
        themeCell.backgroundView = nil;
    }
return themeCell; 

这是我在viewWillAppear 中输入的一些自定义代码:

    self.selectedThemeString = [[NSUserDefaults standardUserDefaults] objectForKey:@"Selection"];

    if ([self.selectedThemeString isEqualToString:@"Original"])
    {
        NSLog(@"1...");
        self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    }
    else if ([self.selectedThemeString isEqualToString:@"Peacock"])
    {
        NSLog(@"2...");

        self.checkedIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    }
    else if ([self.selectedThemeString isEqualToString:@"Mystical"])
    {
        NSLog(@"3...");

        self.checkedIndexPath = [NSIndexPath indexPathForRow:2 inSection:0];
    }
    else if ([self.selectedThemeString isEqualToString:@"Zebra"])
    {
        NSLog(@"4...");

        self.checkedIndexPath = [NSIndexPath indexPathForRow:3 inSection:0];
    }
    else if ([self.selectedThemeString isEqualToString:@"Simplicity"])
    {
        NSLog(@"5...");

        self.checkedIndexPath = [NSIndexPath indexPathForRow:4 inSection:0];
    }
    else if ([self.selectedThemeString isEqualToString:@"Rainbow"])
    {
        NSLog(@"6...");

        self.checkedIndexPath = [NSIndexPath indexPathForRow:5 inSection:0];
    }
    else if ([self.selectedThemeString isEqualToString:@"Prosperity"])
    {
        NSLog(@"7...");

        self.checkedIndexPath = [NSIndexPath indexPathForRow:6 inSection:0];
    }
    else if ([self.selectedThemeString isEqualToString:@"Leopard"])
    {
        NSLog(@"8...");

        self.checkedIndexPath = [NSIndexPath indexPathForRow:7 inSection:0];
    }
    else if ([self.selectedThemeString isEqualToString:@"Hypnotic"])
    {
        NSLog(@"9...");

        self.checkedIndexPath = [NSIndexPath indexPathForRow:8 inSection:0];
    }
    else if ([self.selectedThemeString isEqualToString:@"Dunes"])
    {
        NSLog(@"10...");

        self.checkedIndexPath = [NSIndexPath indexPathForRow:9 inSection:0];
    }
    else if ([self.selectedThemeString isEqualToString:@"Twirl"])
    {
        NSLog(@"11...");

        self.checkedIndexPath = [NSIndexPath indexPathForRow:10 inSection:0];
    }
    else if ([self.selectedThemeString isEqualToString:@"Oceanic"])
    {
        NSLog(@"12...");

        self.checkedIndexPath = [NSIndexPath indexPathForRow:11 inSection:0];
    }


    [self.cView reloadData];
}

所以我可以告诉你,例如,“Zebra”是受影响的单元格之一,它显示为第二行的第一个单元格(我假设它是 indexPathforRow:4 和 inSection:0,因为没有部分。

但是,当我返回时间线并返回 UICollectionView 时,Simplicity 会起作用。

对于这个奇怪的错误的任何指导将不胜感激。

【问题讨论】:

    标签: ios uicollectionview uicollectionviewcell viewwillappear


    【解决方案1】:

    问题已解决。我的错误是不使用该属性来设置图像,而是在 cellForItem 中创建具有相同功能的新图像。问题已修复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 2014-06-13
      • 2018-04-27
      相关资源
      最近更新 更多