【问题标题】:UITableViewCell subclass with UIImageView proper way to reuseUITableViewCell 子类与 UIImageView 正确重用的方法
【发布时间】:2016-06-22 05:59:32
【问题描述】:

我有一个UITableViewCell 子类。我想添加一个UIImageView 作为子视图,但请确保进行适当的重用,这样我就不会一遍又一遍地添加子视图。我还想确保将prepareForReuse 中的图像归零。

这样做的正确方法是什么?

【问题讨论】:

  • 您是否在情节提要中使用原型单元格?
  • @Paulw11 - 不,这些是自定义绘制的单元格。
  • 那么你应该在initWithStyle:resuseIdentifier中添加imageview。您可以将prepareForReuse 或cellForRowAtIndexPath 中的当前图像置零
  • @Paulw11 - 我没有使用initWithStyle:resuseIdentifier。我正在使用dequeueReusableCellWithIdentifier:forIndexPath:。
  • @NicHubbard 为什么使用 viewWithTag 是一种不好的做法。那苹果为什么要引入storyboard原型单元呢。

标签: ios objective-c uitableview


【解决方案1】:

在您的自定义单元子类中,您应该在initWithStyle:reuseIdentifier: 方法中添加任何所需的视图。只要您在表格视图中针对重用标识符注册了单元类,那么每当需要新单元时,dequeueReusableCellWithIdentifier:forIndexPath: 就会调用此初始化程序。重用单元格时不会调用此方法,因此不会多次添加您的图像视图。

可以在cell类的prepareForReuse方法中清除image view的当前图片。

【讨论】:

  • 感谢您的帮助!
【解决方案2】:

为了单元格的正确重用,在 UIViewController 类的 viewDidLoad 中注册单元格 xib 然后写入

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_ID"];

然后在imageView inn cell中加载图片。这样可以保证cell的复用。

【讨论】:

    【解决方案3】:

    您需要将 UIImageView 添加为 UITableviewCell 子类的属性。这样在 cellForRowAtIndexPath 中你就说myCellInstance.profilePicView.image = ...

    请查看我的回答 here,了解我如何将文本字段添加为单元格子视图。具体查看 PersonCell 类中initWithStyle:reusableIdentifier: 的覆盖。在情节提要中不使用原型单元,不使用 viewWithTag,随心所欲。

    如果 Person 类有个人资料图片,则 cellForRowAtIndexPath 如下所示:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        PersonCell *cell = nil;
        cell = [tableView dequeueReusableCellWithIdentifier: @"CellWithNameAndSurname"];
        if(!cell)
        {
            cell = [[PersonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellWithNameAndSurname"];
            cell.contentView.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent: 0.08f];
            cell.delegate = self;
        }
    
        //this should be outside the above if statement!
        Person *respectivePerson = _peopleArray[indexPath.row];
        cell.profilePicView.image = respectivePerson.profilePic;
        cell.nameTextField.text = respectivePerson.name;
        cell.surnameTextField.text = respectivePerson.surname;
        cell.positionLabel.text = [NSString stringWithFormat:@"%i", (int)indexPath.row];
    
        return cell;
    }
    

    【讨论】:

      【解决方案4】:

      重复使用前清洁细胞的正确方法是

      -(void)prepareForReuse;

      只需设置self.imageView.image = nil;

      要创建 UIImageView ,我会这样做:

      - (instancetype)initWithFrame:(CGRect)frame {
         self = [super initWithFrame:frame];
          [self setup];
          return self;
      }
      
      - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
          self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
          [self setup];
          return self;
      }
      
      - (void)setup {
      /// Create your UIImageView and set layout
      }
      

      【讨论】:

      • 我想我还要求位置将UIImageView 添加为子视图,以便每次调用单元格时都不会添加它。
      猜你喜欢
      • 2011-08-06
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多