使用UIImageView更新单元格以显示当前选定的项目

我正在使用滑动视图控制器,将主控制器放在顶部,并使用滑动左侧菜单控制器。

左边的控制器就像Facebook / Pintrest应用程序等菜单一样。左边是一个UITableView

这是我的设置: cellForRowAtIndexPath

 static NSString *CellIdentifier = @"MainMenuCell"; UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; UIView *topSplitterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)]; topSplitterBar.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1]; [cell.contentView addSubview:topSplitterBar]; UIImage *arrowImage = [UIImage imageNamed:@"selectedArrow"]; self.arrowSelectedView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 10, arrowImage.size.width, arrowImage.size.height)]; self.arrowSelectedView.image = arrowImage; [cell.contentView addSubview:self.arrowSelectedView]; self.arrowSelectedView.hidden = YES; } ////// // ADDITIONAL GLUE CODE HERE TO SET LABELS ETC.... ////// if (currentDisplayed) { // This is for the current item that is being displayed cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1]; self.arrowSelectedView.hidden = NO; } else { cell.contentView.backgroundColor = [UIColor colorWithRed:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0]; self.arrowSelectedView.hidden = YES; } NSLog(@"%@",cell.contentView.subviews); return cell; 

表格视图中有两个部分共有7个单元格。 第一节(0)中的一个单元格,第二节(1)中的其余单元格。 有三个显示主控制器的单元格。 一旦他们被选中,我想更新表格视图来显示这个单元格旁边的箭头: 在这里输入图像说明

以下是示例代码: didSelectRowAtIndexPath

  UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ProfileVC"]; __weak typeof(self) weakSelf = self; [self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{ CGRect frame = weakSelf.slidingViewController.topViewController.view.frame; weakSelf.slidingViewController.topViewController = newTopViewController; weakSelf.slidingViewController.topViewController.view.frame = frame; [weakSelf.slidingViewController resetTopViewWithAnimations:nil onComplete:^{ NSIndexPath* displayNameRow = [NSIndexPath indexPathForRow:0 inSection:0]; NSIndexPath* gamesRow = [NSIndexPath indexPathForRow:0 inSection:1]; NSIndexPath* settingsRow = [NSIndexPath indexPathForRow:3 inSection:1]; NSArray* rowsToReload = [NSArray arrayWithObjects:displayNameRow, gamesRow, settingsRow, nil]; [weakSelf.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone]; }]; }]; 

问题/问题所以我在这里有一个奇怪的问题,如果我点击一个不同的单元格,它的作用是在另一个单元格上显示箭头。 然后这再次工作2次,然后第三次随机决定在另一个单元格上显示uiimageview。

即使我通过单元格创build过程中,我看到特定单元格(错误地显示的一个)currentDisplayed的布尔设置为NO,所以它不会将箭头select的视图更改为不隐藏,但它以某种方式吗? 我注销的子视图,可以看到,它是随机不隐藏了,即使对于特定的单元格,它没有设置为不隐藏,所以我认为这是不正确地实现?

这里的主要问题是,在-tableView:cellForRowAtIndexPath:你每次创build一个新的单元格,然后将其存储到一个单一的伊娃,你正在创build一个新的图像视图。 稍后在该方法中设置或隐藏图像视图的调用不能保证(事实上确实不太可能)解决被添加到特定单元的内容视图中的相同图像视图。

一个更方便的地方可以存储在UITableViewCell的视图的子类中,如下所示:

 @interface CustomTableViewCell : UITableViewCell @property (strong, readwrite) UIImageView *imageAccessory; @end @implementation CustomTableViewCell @end @implementation YourClass - (id)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"MainMenuCell"; CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (nil == cell) { cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; UIView *topSplitterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)]; topSplitterBar.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1]; [cell.contentView addSubview:topSplitterBar]; UIImage *arrowImage = [UIImage imageNamed:@"selectedArrow"]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 10, arrowImage.size.width, arrowImage.size.height)]; imageView.image = arrowImage; cell.accessoryImage = imageView; [cell.contentView addSubview: cell.accessoryImage]; cell.accessoryView.hidden = YES; } if (currentDisplayed) { // This is for the current item that is being displayed cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1]; cell.accessoryImage.hidden = NO; } else { cell.contentView.backgroundColor = [UIColor colorWithRed:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0]; cell.accessoryImage.hidden = YES; } return cell; } @end