自定义 UITableViewCell,一个包含两个子视图的 UIView,并且可以滚动

custom UITableViewCell, a UIView that holds two subviews, and scrolling

我有一个自定义的 UITableViewCell,它有一个名为 updateCell 的方法。它添加了一个 spotView,它只是一个 UIView,self.spotView 有几个子视图。

问题是,当我滚动时,spotView 开始时很好,但是当你滚动时,它会填满这些子视图和不准确的信息。我该如何解决这个问题?

我已经尝试在我的自定义 UITableViewCell 中使用 prepareForReuse 并尝试从 spotview 的子视图中删除 FromSuperview 但这也不起作用。

我有,但有点困惑我需要什么才能让它正常工作。我看到我可以将 uilabel 设置为 nil 以准备重用,这似乎可行,但这些 UIView 似乎闲置:

-(void)updateCell:(MenuItem *)item
{
    self.spotView =[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 10.0f, 10.0f)];
    [self.contentView addSubview:self.spotView];


    [self renderHeader:menuItem.header];
    [self renderDetail:menuItem.detail];
    [self renderMeta:menuItem];
    ...
}

- (void)renderMeta:(MenuItem *)menuItem{
  if([self.spotView.subviews count]>0){
    NSLog(@"THERE ARE subviews in spotView in renderMeta for %@", menuItem.header);
  }else{
    NSLog(@"NO subviews in spotView in renderMeta for %@", menuItem.header);
  }


  for (UIView* view in [self.spotView subviews])
  {
    NSLog(@"!!!about to remove subviews here!!!");
        [view removeFromSuperview]; // <- not working
  }


  if([menuItem hasInstoreImage] || [menuItem hasTastingNotes]){
    if([menuItem hasInstoreImage]){
      UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
      NSLog(@"adding instoreImageDot in renderMeta");
      [self.spotView addSubview:instoreImageDot];
    }else{
      NSLog(@"no instoreImageDot in renderMeta");
    }

编辑#1

玩这个,我发现试图操纵自定义 UITableViewCell 中的视图是一个很大的不可预测的事情。我在代码中完成了所有自定义 UITableViewCell,因为我将作为 Cocoapod 在公司内部分发。 DID 的工作是操纵 ViewController 中的单元格,具体如下:

// Does not work on iOS below 6.0
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
   if([cell isKindOfClass:[MenuItemCell class]]){
     NSLog(@"A MenuItem Class");
     MenuItemCell *miCell=(MenuItemCell *)cell;
     [miCell.spotView removeFromSuperview];
   }
}

编辑#2

在哪里/如何调用 updateCell(由于删除了一些代码而有所简化):

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  [self.menuTV registerClass:[MenuItemCell class]forCellReuseIdentifier:@"MenuItemCell"]; 

  static NSString *MenuItemCellIdentifier=@"MenuItemCell";

  id dic=self.menu.listItems[indexPath.row];

  if([dic isKindOfClass:[EMBERSMenuItem class]]){
    //MenuItemCell *cell = [self.menuTV dequeueReusableCellWithIdentifier:MenuItemCellIdentifier];
    MenuItemCell *cell = [self.menuTV dequeueReusableCellWithIdentifier:MenuItemCellIdentifier  forIndexPath:indexPath];

    //MenuItem *menuItem=(MenuItem *)dic;
    MenuItem *menuItem=(MenuItem *)dic;

    cell.menuItem=menuItem; // <- probably shouldn't have this
    [cell updateCell:menuItem];

  }else{

您应该在单元格的 init 方法中将子视图添加到单元格中。否则,如果您在每次渲染单元格时都添加子视图,那么您的单元格将最终拥有与渲染发生一样多的子视图。

举个例子:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:mySubview];
    }
}

然后你可以有一个配置方法来注入你的单元格的数据:

- (void)configureCellWithData:(id)data {
    self.mySubview.setText(data.text);
}