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

时间:2013-05-09 14:59:48

标签: ios ios5 uitableview

我正在使用一个滑动视图控制器,它将主控制器保持在顶部,一个滑动左侧菜单控制器。

左边的控制器就像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)中的其余单元格。有三个单元格在顶部显示主控制器。一旦选中它们,我想更新表格视图,在这个单元格旁边显示一个箭头,如下所示: enter image description here

以下是示例代码: 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更改为不隐藏但它确实以某种方式执行?我注销了子视图,可以看到它已经被随机隐藏了,即使对于那个特定的单元格它没有设置为不隐藏,所以我认为这是以某种方式错误地实现了?

1 个答案:

答案 0 :(得分: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