我使用下面的代码将我的UITableView放在一起。单元格高度允许前三行显示在表格中而不滚动。前三行都一切正常。但是,只要向下滚动到原来的前三行,myImage中的图像将继承前三行中单元格的宽度,并且不会根据基于indexPath.row从数组中提取的值调整大小。在Xcode中动态调整UITableView单元格内的UIImage宽度
该单元显然是从原来绘制的单元格的else语句中重用,但我需要找出一种方法来允许myImage宽度随每行不同而变化。
任何帮助非常感谢! LQ
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
const NSInteger LABEL_TAG = 1001;
const NSInteger IMAGE_TAG = 1002;
UILabel *myLabel;
UIImageView *myImage;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Create the cell
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier]
autorelease];
// Constant values
const CGFloat LABEL_HEIGHT = 20;
const CGFloat LABEL_WIDTH = 140;
const CGFloat LABEL_INDENT = 1;
const CGFloat LABEL_TOP = 0.065 * myTableView.rowHeight;
// Create the label;
myLabel = [[[UILabel alloc] initWithFrame:
CGRectMake(
LABEL_INDENT,
LABEL_TOP + LABEL_HEIGHT,
LABEL_WIDTH,
LABEL_HEIGHT)]
autorelease];
[cell.contentView addSubview:myLabel];
// Configure the label
myLabel.tag = LABEL_TAG;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor blueColor];
myLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 3];
myLabel.textAlignment = UITextAlignmentRight;
// Create the image (NOTE: THIS IS THE PROBLEMATIC PART)
// Extract the width for the image based on an array value for each row:
int xValue = [[myArray objectAtIndex:(indexPath.row)]intValue];
float xLength = (float)xValue/100;
myImage = [[[UIImageView alloc] initWithFrame:
CGRectMake(
LABEL_INDENT + 53, // This places the image to the right of the label
LABEL_TOP + LABEL_HEIGHT,
xLength * LABEL_WIDTH,
//这是其中每一行的宽度被调整 LABEL_HEIGHT] 自动释放];
[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;
}
谢谢这么多为您的快速反应,让我在这个障碍。我只是按照你的例子重建它,它的工作非常出色。 这里是为别人寻找一个解决方案,这种类型的设计元素的附加元素: ... //处理单元格中的文本标签: [(*的UILabel)细胞viewWithTag:SETTINGS_LABEL]的setText:@”设置...“]; //处理单元格中的可变图像宽度: [(UIImageView *)[单元格视图带标签:IMAGE_TAG] setFrame:CGRectMake( (0.0f,15.0f,xLength * LABEL_WIDTH,20.0f)]; – 2010-05-25 21:52:40
欢迎您考虑将问题标记为已回答;) – RickiG 2010-05-27 14:33:22
当您'alloc'背景变量时,会泄漏内存。 – 2010-07-28 22:24:21