2011-03-13 29 views
3

我的UITableView nedd在第一行中有一個圖像。 什麼問題?當用戶向下滾動桌面視圖時,然後向上滾動,在圖像上顯示其他行的信息! 你知道爲什麼嗎?UITableview中的第一行,與其他行不同

這是我的代碼(我使用一個UITableViewCell)

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
if (indexPath.row != 0) { 
    return 73.0; 
} 
else { 
    return 109.0; 
} 
} 

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    if (indexPath.row == 0) { 
     [[NSBundle mainBundle] loadNibNamed:@"customcell_3" owner:self options:NULL]; 
     cell = cellaNib_with_image; 
    } 
    else { 
     [[NSBundle mainBundle] loadNibNamed:@"customcell_2" owner:self options:NULL]; 
     cell = cellaNib; 
    } 


} 

if (indexPath.row == 0) { 
    UIImage *rowBackground; 
    UIImage *selectionBackground; 
    rowBackground = [UIImage imageNamed:@"image.png"]; 
    selectionBackground = [UIImage imageNamed:@"image.png"]; 
    cell.backgroundView = [[[UIImageView alloc] init] autorelease]; 
    ((UIImageView *)cell.backgroundView).image = rowBackground; 
    cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease]; 
    ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground; 

} 
else { 
     NSString *elemento = [NSString stringWithFormat:@"%@", [array objectAtIndex:indexPath.row]]; 
    UILabel *testoLabel = (UILabel*)[cell viewWithTag:1]; 
    testoLabel.text = elemento; 
//ecc... here a take the other datas 
} 
return cell; 
} 

謝謝!

回答

10

當單元格滾動時,它們被重用。

因此,第一個單元可以被重新用於其他單元,反之亦然。

我會使用兩個CellIdentifiers,一個用於第一行,第二個用於其餘的行。

如果indexPath.row == 0,則使用CellID1創建/出隊單元,然後配置並返回。

如果indexPath.row> 1,使用CellID2創建/出列,配置並返回。

如果您想要繼續使用單個cellID,那麼在配置它們之前,先刪除/重置所有內容,以刪除先前的數據。

+0

對!!!非常感謝你!!! :) – JAA

+0

你需要將你只使用一次的細胞出列嗎? –

相關問題