本文介绍了使用UIImageView更新单元格以显示当前选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a sliding view controller which holds the main controller on top and a slide left menu controller.

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

The controller on the left works as the menu like Facebook/Pintrest apps etc. It is a UITableView on the left.

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

There are a total of 7 cells in 2 sections for the table view. One cell in section 1 (0) and the rest in section 2 (1). There are three cells which show a main controller on the top. Once they are selected I would like to update the table view to show an arrow next the cell like this:

以下是示例代码: 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。

Issue/QuestionSo I have a strange issue here, were if I click on a different cell it works the arrow is shown on the other cell. Then again this works for 2 more times, then the third times it randomly decides to show the uiimageview on another cell.

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

Even if I step through the cell creation process I see for that specific cell (the incorrectly displayed one) the boolean for currentDisplayed is set to NO, so it doesn't change the arrowSelectedView to not hidden yet it does somehow? I log out the subviews and can see that it is randomly not Hidden anymore even though for that specific cell it is not set to not hidden, so I am thinking this is implemented incorrectly somehow?

推荐答案

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

The main issue here is that in -tableView:cellForRowAtIndexPath: you're creating a new image view every time you create a new cell, then storing it into a single ivar. The call to set or hide the image view later in that method is not guaranteed (indeed is actually quite unlikely) to be addressing the same image view that was added into that particular cell's content view.

一个更方便的存储位置是UITableViewCell视图的子类,如下所示:

A more convenient place to store it would be in a subclass of UITableViewCell's view like so:

@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

这篇关于使用UIImageView更新单元格以显示当前选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:43