1

我正在使用一个滑动视图控制器,它将主控制器放在顶部和一个向左滑动的菜单控制器。

左侧的控制器用作 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;

表格视图的 2 个部分中共有 7 个单元格。第 1 (0) 节中的一个牢房,第 2 (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。

即使我逐步完成单元格创建过程,我看到该特定单元格(显示不正确的单元格)的布尔值 currentDisplayed 设置为 NO,因此它不会将 arrowSelectedView 更改为未隐藏但它以某种方式执行?我注销子视图,可以看到它不再是随机隐藏的,即使对于那个特定的单元格它没有设置为不隐藏,所以我认为这是以某种方式错误地实现?

4

1 回答 1

0

这里的主要问题是,在 -tableView:cellForRowAtIndexPath: 中,每次创建新单元格时都会创建一个新的图像视图,然后将其存储到单个 ivar 中。稍后在该方法中设置或隐藏图像视图的调用不能保证(实际上实际上不太可能)处理添加到该特定单元格的内容视图中的相同图像视图。

存储它的更方便的地方是 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
于 2013-05-09T15:37:20.677 回答