我使用下面的代碼將我的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