2012-01-10 87 views
0

我有對象 「Consumo」的NSMutableArray和UITableView的(EXC_BAD_ACCESS)

-(id)initWithDictionary:(NSDictionary *)consumo{ 
    self = [super init]; 

    if (self != nil) 
    { 
     fechaLectura = [consumo objectForKey:@"fechaLectura"]; 
     tarifa = [consumo objectForKey:@"tarifa"]; 
     consumoBase = [consumo objectForKey:@"consumoBase"]; 
     consumoHP = [consumo objectForKey:@"consumoHP"]; 
     reactivoLeido = [consumo objectForKey:@"reactivoLeido"]; 
     reactivoFacturado = [consumo objectForKey:@"reactivoFacturado"]; 
     demandaFPLeida = [consumo objectForKey:@"demandaFPLeida"]; 
     demandaFPFacturada = [consumo objectForKey:@"demandaFPFacturada"]; 
     demandaHPLeida = [consumo objectForKey:@"demandaHPLeida"]; 
     demandaHPFacturada = [consumo objectForKey:@"demandaHPFacturada"]; 
     calificacion = [consumo objectForKey:@"consumo"]; 
    } 
    return self; 
} 

viewController.h

@interface ConsumoViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 
    IBOutlet UILabel * sumLabel; 
    NSMutableArray * consumos; 
    IBOutlet UITableView *tablaConsumo; 
    NSMutableArray * titulosConsumo; 
    Consumo * cons; 
} 

@property (nonatomic,retain) IBOutlet UILabel * sumLabel; 
@property (nonatomic,retain) NSMutableArray * consumos; 
@property (nonatomic,retain) UITableView * tablaConsumo; 
@property (nonatomic,retain) NSMutableArray * titulosConsumo; 
@property (nonatomic,retain) Consumo * cons; 
@end 

viewcontroller.m(包括的tableview)

- (void)viewDidLoad 
{ 
     consumos = [[NSMutableArray alloc] init]; 
     NSDictionary * dict; 

     for (int i = 0; i < [suministro count]; i++){ /* suministro = array to dictionary */ 

      dict=[suministro objectAtIndex:i]; 
      cons = [[Consumo alloc] initWithDictionary:dict]; 
      [consumos insertObject:cons atIndex:i]; 
     } 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 

detFechaLectura = [[[UILabel alloc] initWithFrame:CGRectMake(10.0, 20.0, 120.0, 20.0)] autorelease]; 
     detFechaLectura.tag = LABELSUP1; 
[cell.contentView addSubview:detFechaLectura]; 
}else{ 
    detFechaLectura = (UILabel *)[cell.contentView viewWithTag:LABELSUP1]; 
} 

detFechaLectura.text = [[consumos objectAtIndex:indexPath.row] fechaLectura]; 

return cell 

} 

}

問題:

detFechaLectura.text = [[consumos objectAtIndex:indexPath.row] fechaLectura]; (主題1:程序接收信號:「EXC_BAD_ACCESS」)

我希望我能幫上忙。謝謝

回答

1

這是關於內存管理。您需要retaininit方法從你的字典中的對象:

fechaLectura = [[consumo objectForKey:@"fechaLectura"] retain]; 

如果你的屬性設置爲保留,則必須使用self.訪問這樣的:

self.fechaLectura = [consumo objectForKey:@"fechaLectura"]; 
+0

很好,謝謝dealloc方法。我想過這樣做: @property(nonatomic,retain)NSString * fechaLectura;保留要計算初始化了多少次。 – JohnPortella 2012-01-10 15:41:24

+0

看我的編輯:)如果你使用'self.fechaLectura',那麼你正在使用屬性(因此保留)。如果你只是使用'fechaLectura',那麼你必須自己保留。 – deanWombourne 2012-01-10 16:20:04

1

你需要分配屬性而不是實例變量,以正確保留您的值。

if (self != nil) 
{ 
    self.fechaLectura = [consumo objectForKey:@"fechaLectura"]; 
    self.tarifa = [consumo objectForKey:@"tarifa"]; 
    self.consumoBase = [consumo objectForKey:@"consumoBase"]; 
    self.consumoHP = [consumo objectForKey:@"consumoHP"]; 
    self.reactivoLeido = [consumo objectForKey:@"reactivoLeido"]; 
    self.reactivoFacturado = [consumo objectForKey:@"reactivoFacturado"]; 
    self.demandaFPLeida = [consumo objectForKey:@"demandaFPLeida"]; 
    self.demandaFPFacturada = [consumo objectForKey:@"demandaFPFacturada"]; 
    self.demandaHPLeida = [consumo objectForKey:@"demandaHPLeida"]; 
    self.demandaHPFacturada = [consumo objectForKey:@"demandaHPFacturada"]; 
    self.calificacion = [consumo objectForKey:@"consumo"]; 
} 

而在你需要釋放那些每一個沒有self.

-(void)dealloc 
{ 
    [fechaLectura release]; 
    // ... 
    [calificacion release]; 

    [super dealloc]; 
} 
+0

是否需要消除consumo.m?還不夠: [利潤釋放]在ViewController? – JohnPortella 2012-01-10 15:51:13

+0

我不知道你的意思是消除consumo.m。但是,只要你保留一個類的對象,你也需要在dealloc方法中釋放它。 – Joe 2012-01-10 15:56:15

相關問題