1

我正在尝试设置一个可以在右上角有图像的 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 =”行,图像确实以横向显示......尽管它不会转移到最右边。

如果我在这里做错了什么,请告诉我。

提前感谢您的任何帮助。

4

2 回答 2

2

我想通了。解决方案是创建两个 UIImageView... 一个用于星角图像,一个用于单元格背景图像。

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

星形 UIImageView 使用以下方法正确定位自身:

starImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

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

于 2010-08-20T19:50:34.550 回答
0

你的 UIViewController 应该响应 didRotateFromInterfaceOrientation。

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

祝你好运!

于 2010-08-18T04:22:18.903 回答