Updating cells with UIImageView to show currently selected item

半腔热情 提交于 2020-01-05 08:00:24

问题


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

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

Here is my setup: 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;

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:

Here is the example code for: 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];
            }];
        }];

Issue/Question So 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.

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?


回答1:


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.

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


来源:https://stackoverflow.com/questions/16464898/updating-cells-with-uiimageview-to-show-currently-selected-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!