2011-01-30 15 views
0

我正在創建一個應用程序,其中有許多表格單元格(> 300)。我已經成功實現了展開和摺疊tableview單元格。展開時,顯示的是從web服務中獲取的圖像,單擊展開的單元格時,單元格會再次摺疊,如果單擊了另一個單元格,則先前單元格摺疊並且當前單元格展開。現在的問題是,如果用戶使用單元格展開表格滾動tableview時效果不錯,但是當他返回 時,圖像會丟失,並且功能也有一些問題(表格會將該單元格摺疊)。滾動後會丟失Expanded TableView中的圖像

此外,如果用戶不斷向下滾動表格,他可以在collapased單元格中看到該圖像(這看起來非常糟糕),我認爲它是因爲dequeueReusableCellWithIdentifier正在獲取已擴展的單元格。 PS:每個圖像都是基於單元格值而不同,但它是在不同的調用中獲取的(不是一起調用,即獲取300張圖像,我可以調用300次獨立調用)。

回答

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UIImageView * imageView; 
    UILabel  * nameLabel; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier] autorelease]; 

     nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(45.0, 5.0, 245.0, 34.0)]; 
     nameLabel.font = [UIFont fontWithName:CELL_FONT size:CELL_FONT_SIZE]; 
     nameLabel.tag = 2; 
     nameLabel.backgroundColor = [UIColor clearColor]; 
     [cell.contentView addSubview:nameLabel]; 
     [nameLabel release]; 

     imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 5.0, 30.0, 30.0)]; 
     imageView.tag = 1; 
     imageView.contentMode = UIViewContentModeScaleAspectFit; 
     [cell.contentView addSubview:imageView]; 
     [imageView release]; 
    } 
    else { 
     imageView = (id)[cell.contentView viewWithTag:1]; 
     nameLabel = (id)[cell.contentView viewWithTag:2]; 
    } 

    return cell; 
}