ios - 自定义 UITableViewCell,一个包含两个 subview 的 UIView,并且可以滚动

标签 ios uitableview

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

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

我已经尝试在我的自定义 UITableViewCell 中使用 prepareForReuse 并尝试从 spotview 的 subview 中删除 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 中的 View 是一个很大的不可预知的过程。我在代码中完成了所有自定义 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 方法中将 subview 添加到单元格中。否则,如果您在每次渲染单元格时都添加 subview ,那么您的单元格将最终拥有与渲染发生一样多的 subview 。

举个例子:

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

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

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

关于ios - 自定义 UITableViewCell,一个包含两个 subview 的 UIView,并且可以滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28337136/

相关文章:

ios - 在 iOS 9 中未调用 UITableViewCell 中的 IBAction

iphone - 动态调整表格的高度

ios - 使用swift 3如何从firebase中检索另一个数据中的数据

ios - 在一个特定的 UIView 旋转时防止动画

ios - for循环的swift 3.0索引1超出范围[0 .. 0]错误

javascript - iOS 8 页面在输入焦点上跳转

ios - 可定制的表格 View 单元格滑动以停止完全展开?

ios - 必须在 uitableviewcell 中双击才能打开 View

iphone - 使用 json 文件折叠展开标题部分

ios - 需要使用 Swift 在 UITableView 中的每一行之后显示部分