Iphone 在Xcode中的UITableView单元格中动态调整UIImage宽度

Iphone 在Xcode中的UITableView单元格中动态调整UIImage宽度,iphone,xcode,uitableview,width,cell,Iphone,Xcode,Uitableview,Width,Cell,我正在使用下面的代码来组合我的UITableView。单元格高度允许在表格中显示前三行,而无需滚动。前三排都很好。但当我向下滚动过原始的前三行时,myImage中的图像将继承前三行中单元格的宽度,并且不会根据从基于indexath.row的数组中提取的值调整大小 该单元格显然是从最初绘制的单元格的else语句中重用的,但我需要找出一种方法,允许每行的myImage宽度不同 非常感谢您的帮助! lq //这是调整每行宽度的地方 标签高度] 自动释放] [cell.contentView a

我正在使用下面的代码来组合我的UITableView。单元格高度允许在表格中显示前三行,而无需滚动。前三排都很好。但当我向下滚动过原始的前三行时,myImage中的图像将继承前三行中单元格的宽度,并且不会根据从基于indexath.row的数组中提取的值调整大小

该单元格显然是从最初绘制的单元格的else语句中重用的,但我需要找出一种方法,允许每行的myImage宽度不同

非常感谢您的帮助! lq

//这是调整每行宽度的地方 标签高度] 自动释放]

    [cell.contentView addSubview:myImage];

    myImage.contentMode = UIViewContentModeLeft;
    myImage.image = [UIImage imageNamed:@"ProgressBar.png"];
    myImage.clipsToBounds = YES;

} else {

    // Re-use cells

    myLabel = (UILabel *)[cell viewWithTag:LABEL_TAG];
    myImage = (UIImageView *)[cell viewWithTag:IMAGE_TAG];  
}

return cell;

}

正如您在评论中所提到的,上面的代码重用了单元格,但是;找到缓存的单元格后,无论该单元格是旧的还是新的,都要重新进行设置

UITableViewCell是一个“两方”,第一次引用单元格时,它将被实例化并生成,随后所有被引用时,它只应被更新。(单元格每次滚动到屏幕上或重新加载tableview时都会被引用。这种情况经常发生,因此为了节省CPU时间,最好不要反复进行相同的设置)

所以,试着像这样接近它:

- (void) configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    UILabel *settingText = [ISInterfaceElement getLabel:Headline]; //Opps... ISInterfaceElement is my custom class. All it does is return a UILabel with settings that comply for a Headline label, according to an enum in the header.
    [settingText setFrame:CGRectMake(0.0f, 15.0f, 320.0f, 20.0f)];
    [settingText setTextAlignment:UITextAlignmentCenter];
    [settingText setTag:SETTINGS_LABEL];
[cell addSubview:settingText];
    UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 50.0f)];
    [cell addSubView:background]; //added
    [background release]; //added

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];     
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {       
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        [self configureCell:cell atIndexPath:indexPath];
    }


    [(UILabel*)[cell viewWithTag:SETTINGS_LABEL] setText:@"Settings…"];

    return cell;
}
因此,如果cellForRowAtIndexPath需要一个新的单元格设置,那么它将调用configureCell方法,否则它只需 使用模型中的正确值更新单元格(通常为[someArray objectAtIndex:indexPath.row],但在我的示例中,仅为字符串

因此,发送configureCell方法需要知道的任何参数(高度),以便能够构建单元,并在该方法中进行所有构建,在CellForRowatinex中进行所有更新


希望它有意义:)

非常感谢您的快速响应,帮助我克服了这一障碍。我只是按照你的例子重建了它,它工作得非常出色。这是其他任何寻找此类设计元素解决方案的人的附加元素:…/处理单元格中的标签文本:[(UILabel*)[Cell viewWithTag:SETTINGS_Label]setText:@“SETTINGS…”;//处理单元格中的可变图像宽度:[(UIVIVIEVIEW*])[单元格视图标签:IMAGEYTAG]设置框:CGRectMake((0.0F,150F,xSale*LabelSuff-Field20.0f);欢迎将问题标记为回答;)当<代码> OLC/ <代码>背景< /代码>变量时,你泄漏内存。@ Shaggy,谢谢,你当然是正确的。我还忘了将背景视图添加到单元格。。。
- (void) configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    UILabel *settingText = [ISInterfaceElement getLabel:Headline]; //Opps... ISInterfaceElement is my custom class. All it does is return a UILabel with settings that comply for a Headline label, according to an enum in the header.
    [settingText setFrame:CGRectMake(0.0f, 15.0f, 320.0f, 20.0f)];
    [settingText setTextAlignment:UITextAlignmentCenter];
    [settingText setTag:SETTINGS_LABEL];
[cell addSubview:settingText];
    UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 50.0f)];
    [cell addSubView:background]; //added
    [background release]; //added

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];     
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {       
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        [self configureCell:cell atIndexPath:indexPath];
    }


    [(UILabel*)[cell viewWithTag:SETTINGS_LABEL] setText:@"Settings…"];

    return cell;
}