【问题标题】:NSMutableArray and UITableVIew ( exc_bad_access)NSMutableArray 和 UITableVIew (exc_bad_access)
【发布时间】:2012-02-07 00:09:36
【问题描述】:

我有对象“Consumo”

-(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”)

希望能帮上忙。谢谢

【问题讨论】:

    标签: objective-c xcode4 ios5 exc-bad-access


    【解决方案1】:

    这与内存管理有关。您需要在您的 init 方法中 retain 字典中的对象:

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

    如果您的属性设置为保留,那么您必须使用self. 来访问它:

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

    【讨论】:

    • 太好了,谢谢。我想过这样做:@property(nonatomic,retain)NSString * fechaLectura; Retain 会计算初始化的次数。
    • 查看我的编辑 :) 如果您使用 self.fechaLectura,那么您正在使用该属性(因此是保留)。如果你只使用fechaLectura,那么你必须自己做。
    【解决方案2】:

    您需要分配属性而不是实例变量,才能正确保留您的值。

    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"];
    }
    

    并且在 dealloc 方法中,您需要释放没有self.的每一个

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

    【讨论】:

    • 有必要消除 consumo.m 吗?还不够:ViewController 中的 [cons release]?
    • 我不确定您所说的消除 consumo.m 是什么意思。但是每当你为一个类保留一个对象时,你还需要在 dealloc 方法中释放它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多