2010-05-25 120 views
0

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

} 

回答

1

上面重用了單元格,因爲您在註釋中引起注意,但是;找到一個緩存的單元格後,再次進行設置,不管單元是舊的還是新的。

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方法知道能夠建立你的細胞,並做所有的建設該方法和cellForRowAtIndex中的所有更新。

希望它是有道理的:)

+0

謝謝這麼多爲您的快速反應,讓我在這個障礙。我只是按照你的例子重建它,它的工作非常出色。 這裏是爲別人尋找一個解決方案,這種類型的設計元素的附加元素: ... //處理單元格中的文本標籤: [(*的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

+0

歡迎您考慮將問題標記爲已回答;) – RickiG 2010-05-27 14:33:22

+0

當您'alloc'背景變量時,會泄漏內存。 – 2010-07-28 22:24:21