Iphone 如何在自定义单元格中居中放置图像

Iphone 如何在自定义单元格中居中放置图像,iphone,ios,Iphone,Ios,在我的自定义单元格中,我想将图像居中,但这不起作用 self.leftImage = [[[UIImageView alloc] initWithImage:cry] autorelease] ; float h=cell.frame.size.height; float w=cell.frame.size.width; CGRect r = leftImage.frame; r.origin = cell.frame.origin; r.ori

在我的自定义单元格中,我想将图像居中,但这不起作用

    self.leftImage = [[[UIImageView alloc] initWithImage:cry] autorelease] ;
    float h=cell.frame.size.height;
    float w=cell.frame.size.width;

    CGRect r = leftImage.frame;
    r.origin = cell.frame.origin;
    r.origin.x = w/ 2  - r.size.width / 2;
    r.origin.y = h / 2  - r.size.height / 2 + 12;
    leftImage.frame = r;
            [self.contentView addSubview:leftImage];

我想您在创建新单元格时,或者在init部分的单元格本身中,尝试在
-(UITableViewCell*)tableView:(UITableView*)tableViewRef cellforrowatinexpath:(nsindepath*)indepath
中执行此操作。此时单元格没有正确的帧,您不能依赖它。

尝试设置
leftImage
autoresizingMask

self.leftImage.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin
创建单元时,您永远无法确定其尺寸。大多数情况下,单元格将使用
CGRectZero
框架初始化,然后,
UITableView
将适当调整单元格大小。

我在(id)initWithCellIdentifier:(NSString*)cellID中进行了此操作