ios - 如何设置这个自定义 UITableViewCell

标签 ios uitableview

我接手了一个 iOS 项目,必须将 View 列表重构为 UITableView。我正在使用 Storyboards 并将 UITableViewCell 子类化。一个子类称为 MenuItemCell,它有一个 headerLabel、detailLabel 和 priceLabel,它们是在 Storyboard 中设置并在 MenuItemCell 中配置的属性。我可以像这样通过 cellForAtIndexPath 操作它们:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  static NSString *MenuItemCellIdentifier=@"MenuItemCell";
  id dic=self.tmpMenu.listItems[indexPath.row];
  if([dic isKindOfClass:[MenuItem class]]){
    MenuItemCell *cell = [self.menuTV dequeueReusableCellWithIdentifier:MenuItemCellIdentifier];
    MenuItem *menuItem=(MenuItem *)dic;
    cell.menuItem=menuItem;
    
    cell.headerLabel.text=menuItem.header;
    cell.headerLabel.numberOfLines=0;

    cell.priceLabel.text=menuItem.price;

     // how to handle this custom spotView
     if([menuItem hasInstoreImage]){
        UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
        [cell.spotView addSubview:instoreImageDot];  // ON SCROLLING, this populates to all the different table cells
      }

    return cell;
  }
  return nil;
}

最后一 block 是有一个自定义的UIView叫spotView。目前,我正在通过 circleWithColor 在我的 Controller 中的代码中创建这个圆圈并尝试添加到 [cell.spotView] 但滚动导致它填充在不同的表格单元格上。我应该如何设置?我已经向我的自定义 View 添加了一个方法,但这也遇到了同样的问题。

最佳答案

单元格被重用,您需要告诉 tableView 删除自定义 View

 if([menuItem hasInstoreImage]){
    UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
    [cell.spotView addSubview:instoreImageDot]; 
  }else{
    //remove it if condition is not met
    //or You can add a place holder view instead
  }

关于ios - 如何设置这个自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25410711/

相关文章:

ios - 如何将自动续订订阅与登录用户映射

ios - 对 UITableView 部分使用一个 NSManagedObject,它与另一个 NSManagedObject 的行有关系吗?

uitableview - missViewControllerAnimated 卡住应用程序

ios - 单元格中的 UIButton 没有改变

iOS - 约束 "Margins"警告 - UITableViewCell 内的 UIView

ios - 将 UITextField 文本传递给不同 View Controller 上的 UILabel

ios - 什么是 iOS 5 SDK 中的容器 View ?

ios - AFNetworking 3.0如何下载文件并保存到本地?

ios - 使用 UnwantedCommunicationReportingExtension 时如何设置 ILClassificationResponse 的内容?

ios - 存储的数据在 TableView 中无法显示,在核心数据的一对多关系中?