代码之家  ›  专栏  ›  技术社区  ›  Jim Rhoades

当旋转到横向时,为什么UIImage会从UITableViewCell中消失?

  •  1
  • Jim Rhoades  · 技术社区  · 16 年前

    我正在尝试设置一个UITableViewCell,它可以在右上角有一个图像。

    我有它的工作肖像模式,但当我旋转到横向,图像消失。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
            UIImage *cornerImage = [UIImage imageNamed:@"star_corner.png"];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.bounds.size.width - cornerImage.size.width,
                                                                                   0,
                                                                                   cornerImage.size.width,
                                                                                   cornerImage.size.height)];
            imageView.tag = kCornerImageViewTag;
            [cell.contentView addSubview:imageView];
            [imageView release];
        }
    
        UIImageView *theView = (UIImageView *)[cell.contentView viewWithTag:kCornerImageViewTag];
        [theView setImage: [UIImage imageNamed:@"star_corner.png"]];
    
        cell.textLabel.text = @"the text label";
        return cell;
    }
    

    有趣的是,如果我注释掉“cell.textLabel.text=”行,图像确实显示在横向中。。。虽然它不会移到最右边。

    如果还有什么我做错的,请告诉我。

    提前感谢您的帮助。

    2 回复  |  直到 16 年前
        1
  •  2
  •   Jim Rhoades    16 年前

    我想出来了。解决方法是创建两个UIImageView的。。。一个用于星角图像,一个用于细胞背景图像。

    我将星角UIImageView作为子视图添加到背景图像UIImageView中,然后将其指定给cell.backgroundView。

    starImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    

    而且由于整个内容都被分配给cell.backgroundView(而不是cell.contentView),因此它不受单元格的delete按钮或重新排序控件的影响。

        2
  •  0
  •   rjobidon    16 年前

    在这个方法中,您可以遍历单元格,使用[cell viewWithTag]获取UIImageView,并根据新的单元格维度(cell.contentView.bounds)修改frame属性。