2009-12-03 44 views
1

我使用UITableViewController顯示一些數據,我的表格有2個靜態部分,每個部分有6個靜態行。而且我在繼承UITableViewCell以添加3個標籤和一個視圖,在視圖中,我只在其中一個部分的一個單元格中繪製箭頭。UITableView滾動更改UITableViewCell裏面的圖像

這一切都很順利。直到我向下滾動...然後箭頭隨機放置在其他單元格中,一旦我向後滾動箭頭再次更改單元格...這也發生如果我嘗試設置backgroundColor只有一個單元格。一旦我向下滾動,原本已經有顏色的細胞不再有它,另一個看似隨機的細胞現在具有這種顏色。

下面是我的一些代碼:

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

static NSString *CellIdentifier = @"Cell"; 

TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[TableViewCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
[self configureCell:cell atIndexPath:indexPath]; 


return cell; 

}

- (void)configureCell:(TableViewCells *)cell atIndexPath:(NSIndexPath *)indexPath { 
switch (indexPath.row) { 
    case 0: 
    { 
     if (indexPath.section == cotizacionIdeal) 
     { 
      cell.arrowImage = [UIImage imageNamed:@"arrowUp.png"]; 
      [cell.nombre setText:@"Recursos"]; 
      [cell.cantidad setText:[NSString stringWithFormat:@"%1.2f", [cotizacionPrevia.sueldoDeRecursos doubleValue]]]; 
      [cell.porcentaje setText:[NSString stringWithFormat:@"%1.2f%@", [cotizacion.recursosPorcentaje doubleValue], @"%"]]; 
     } 
     else 
     { 
      [cell.nombre setText:@"Recursos"]; 
      [cell.cantidad setText:[NSString stringWithFormat:@"%1.2f", [cotizacionPrevia.sueldoDeRecursos doubleValue]]]; 
      [cell.porcentaje setText:[Calculate calcPorcentaje:cotizacionPrevia.sueldoDeRecursos totalReal:self.totalReal]]; 
     } 
    } 
     break; 
    case 1: 
    { 
     if (indexPath.section == cotizacionIdeal) 
     { 
      [cell.nombre setText:@"Comision de Venta"]; 
      [cell.cantidad setText:[Calculate calcMonto:cotizacion.comisionPorcentaje total:self.totalIdeal]]; 
      [cell.porcentaje setText:[NSString stringWithFormat:@"%1.2f%@", [cotizacion.comisionPorcentaje doubleValue], @"%"]]; 
     } 
     else 
     { 
      [cell.nombre setText:@"Comision de Venta"]; 
      [cell.cantidad setText:self.montoRealComision]; 
      [cell.porcentaje setText:[NSString stringWithFormat:@"%1.2f%@", [cotizacion.comisionPorcentaje doubleValue], @"%"]]; 
     } 
    } 

UITableViewCell類:

- (id)initWithFrame:(CGRect)frame cell:(TableViewCells *)cell 
{ 
    if (self = [super initWithFrame:frame]) { 
     _cell = cell; 

     //self.opaque = YES; 
     //self.backgroundColor = _cell.backgroundColor; 
    } 

    return self; 
} 

- (void)drawRect:(CGRect)rect 
{ 
    [_cell.arrowImage drawAtPoint:CGPointMake(0, 0)]; 
} 

@end 

@implementation TableViewCells 

@synthesize nombre; 
@synthesize porcentaje; 
@synthesize cantidad; 
@synthesize arrowImage; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 
     // Initialization code 

     UILabel *nombreLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     self.nombre = nombreLabel; 
     [nombreLabel release]; 
     [self.nombre setFont:[UIFont boldSystemFontOfSize:14]]; 
     [self.contentView addSubview:self.nombre]; 

     UILabel *porcentajeLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     self.porcentaje = porcentajeLabel; 
     [porcentajeLabel release]; 
     [self.porcentaje setFont:[UIFont italicSystemFontOfSize:10]]; 
     [self.contentView addSubview:self.porcentaje]; 

     UILabel *cantidadLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     self.cantidad = cantidadLabel; 
     [self.cantidad setTextAlignment:UITextAlignmentRight]; 
     [cantidadLabel release]; 
     [self.cantidad setFont:[UIFont boldSystemFontOfSize:14]]; 
     [self.contentView addSubview:self.cantidad]; 

     cellContentView = [[TableViewCellContentView alloc] initWithFrame:CGRectZero cell:self]; 
     cellContentView.backgroundColor = [UIColor clearColor]; 
     [self.contentView addSubview:cellContentView]; 
    } 

UPDATE:

我自己也嘗試設置圖像一個不同除了一個人以外,每個人都有一個,我仍然有同樣的問題。

- (void)configureCell:(TableViewCells *)cell atIndexPath:(NSIndexPath *)indexPath { 
cell.arrowImage = [UIImage imageNamed:@"arrowDown.png"]; 
switch (indexPath.row) { 
    case 0: 
    { 
     if (indexPath.section == cotizacionIdeal) 
     { 
      cell.arrowImage = [UIImage imageNamed:@"arrowUp.png"]; 
      [cell.nombre setText:@"Recursos"]; 
      [cell.cantidad setText:[NSString stringWithFormat:@"%1.2f", [cotizacionPrevia.sueldoDeRecursos doubleValue]]]; 
      [cell.porcentaje setText:[NSString stringWithFormat:@"%1.2f%@", [cotizacion.recursosPorcentaje doubleValue], @"%"]]; 
     } 
     else 
     { 

回答

1

的問題是,只要您使用的drawRect的細胞必須調用setneedsdisplay。這是解決我的問題。

0

的問題是UITableView的重用細胞,使其不需要做出比顯示出更多的情況下,在你configureCell方法你沒有明確刪除的箭頭。

如果箭頭不應顯示在單元格上,請在configureCell方法中明確地將其清除,並且應該修復它。

+0

你好本,謝謝你的回答,我知道,視單元正在被重用,我試圖明確清除cell.arrowImage屬性設置爲零。然而,這不太好。 即使我爲每個單元格設置不同的圖像,一旦向下滾動,圖像就會混淆。我在每個單元格中設置圖像的方式與第一個單元格相同。我應該如何處理這個問題?先謝謝你。 -Oscar – 2009-12-03 18:55:24

+0

我還試圖此形式給出 - (無效)configureCell:(TableViewCells *)細胞atIndexPath:(NSIndexPath *)indexPath { \t cell.arrowImage = [UIImage的imageNamed:@ 「arrowDown.png」]; \t開關(indexPath.row){ \t \t情況下0: \t \t { \t \t \t如果(indexPath。部== cotizacionIdeal) \t \t \t { \t \t \t \t cell.arrowImage = [UIImage的imageNamed:@ 「arrowUp.png」]; \t \t \t \t [cell.nombre setText:@「Recursos」]; \t \t \t \t [cell.cantidad setText:[NSString stringWithFormat:@「%1.2f」,[cotizacionPrevia.sueldoDeRecursos doubleValue]]]]; 這工作正常,直到我向下滾動。 – 2009-12-03 18:59:25

+0

我想知道這是因爲我使用iPhone模擬器? – 2009-12-03 19:32:22

0

我不是100%,但我認爲這是關於圖像的發佈。當你使用imageNamed時,實例被保留,所以建議使用imageWithFilePath來代替。

希望這有助於....

相關問題