【问题标题】:Cell image only appearing after scrolling单元格图像仅在滚动后出现
【发布时间】:2012-08-09 11:43:11
【问题描述】:

我有一个 UITableView,其方法如下所示 cellForRowAtIndexPath,问题是 cellImage 仅在完全向上滚动(因此没有行可见)然后释放后才出现。奇怪的是,我在另一个类中有相同的代码用于单独的表格视图,并且图像看起来很好。有什么想法吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    [cell.textLabel setText:[items objectAtIndex:indexPath.row]];
    [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:15.0f]];
    [cell.textLabel setTextColor:[UIColor whiteColor]];
    [cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundView:[[UIView alloc] init]];
    [self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
    UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next@2x.png"]];
    [cell addSubview:cellImage];
    [cellImage setFrame:CGRectMake(0, 0, 26, 24)];
    [cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)];

    return cell;
}

【问题讨论】:

  • 不是真正的问题相关,但为什么是:[self.tableView setBackgroundView:nil]; [self.tableView setBackgroundView:[[UIView alloc] init]]; [self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];在您的 cellforrow 委托方法中?不建议去那里
  • 为什么要将 cellImage 添加到单元格作为默认单元格有 imgView 你可以像这样使用 cell.imgView.image = yourImage
  • 如果你想显示点击单元格然后使用单元格的附件来显示指示器并删除你的 imageView
  • @Totumus Maximus 出于某种原因,这是我找到的唯一一个添加代码的地方
  • 使用 UITableView 已有的 imageView 属性。另外,在设置框架后直接设置中心确实没有意义。但是,这不会造成伤害,并且与您的问题无关。

标签: iphone ios ipad uitableview uiimageview


【解决方案1】:
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundView:[[UIView alloc] init]];

这些行不属于该方法。每次滚动每个单元格时都会调用这些方法。创建表格视图后,将它们放入 viewDidLoad 方法中。

UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next@2x.png"]];
[cell addSubview:cellImage];

这些行真的非常BAD,可能会导致内存泄漏。

每次滚动表格视图时,都会向单元格添加一个图像视图。表视图重用单元格。这意味着当单元格被重用时,您向单元格添加一个新的图像视图。滚动更多次后,每个单元格上都有 x 次 imageview,除非您在代码中的某处删除它们。

另外一点是,如果你想将一些子视图添加到单元格中(例如,在单元格的 init 方法中)然后将其添加到单元格的内容视图中

[cell.contentView addSubview:someView];

否则您的子视图可能会与附件视图重叠。调整单元格大小时可能会出现问题。

我建议您阅读以下链接中的指南: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1

【讨论】:

    【解决方案2】:

    正如许多 cmets 所捕获的,存在许多问题:

    在 viewDidLoad 方法(或通过 XIB)中设置视图和 tableView 背景,而不是在 cellForRowAtIndexPath 中

    使用 cell.imageView 属性设置左图标,或者,构建自定义单元格,您将使用 [cell.contentView addSubview:cellImage]。如果您使用后者,您还应该以与 UILabel 相同的方式添加您的 textLabel。

    要设置表格视图单元格的背景,请使用此委托方法:

    -(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
     [cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]]; 
    }
    

    虽然为了提高效率,您应该在视图控制器中实例化一次此 UIColor 并将其设置为 [cell setBackgroundColor:self.cellBackground];

    【讨论】:

      【解决方案3】:

      您面临的问题是由于单元格的重用标识符。 此外,每次执行 cellForRowAtIndexPath 时,您都会创建 Imageview。

      我会在这里为你推荐两个解决方案。

      第一个选项:- 只需替换此代码:

      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
          cell.accessoryType = UITableViewCellAccessoryNone;
      }
      

      在处理动态图像时,这不是正确的方法,意味着您正在从服务器下载缩略图并且数据内容更多,如果它是小而静态的数据,那么对您来说没问题。

      第二个选项:- 只需使用此格式化代码,这不会每次只使用当前单元格中带有标签的对象时创建图像视图。

          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
      
      
          static NSString *CellIdentifier = @"CellIdentifier";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
              cell.accessoryType = UITableViewCellAccessoryNone;
              UIImageView *cellImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 26, 24)];  
              cellImage.tag = 88;
              [cell addSubview:cellImage];
              [cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)];
      
          }
          [cell.textLabel setText:[items objectAtIndex:indexPath.row]];
          [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:15.0f]];
          [cell.textLabel setTextColor:[UIColor whiteColor]];
          [cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
          [self.tableView setBackgroundView:nil];
          [self.tableView setBackgroundView:[[UIView alloc] init]];
          [self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
          UIImageView *cellImage = (UIImageView *)[cell viewWithtag:88];
          cellImage.image = [UIImage imageNamed:@"next@2x.png"];
      
      }
      

      【讨论】:

      • 对不起,我不知道如何格式化代码,所以看起来很乱。
      • 使用 {} 格式化代码删除 1),2) 编号,这会混淆编辑器
      【解决方案4】:

      使用以下代码修复:

      UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next@2x.png"]];
      [cell setAccessoryView:cellImage];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多