NSMutableArray 和 UITableVIew ( exc_bad_access)

发布于 2024-12-26 12:28:42 字数 2854 浏览 6 评论 0原文

我有对象“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”)

我希望我能提供帮助。谢谢

I have object "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 ( include 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

}

}

Problem:

detFechaLectura.text = [[consumos objectAtIndex:indexPath.row] fechaLectura];
( Thread 1: Program received signal : "EXC_BAD_ACCESS")

I hope I can help. thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-柠檬树下少年和吉他 2025-01-02 12:28:42

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

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

如果您的属性设置为保留,则必须使用 self. 来访问像这样:

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

It's to do with memory management. You need to retain the objects from your dictionary in your init method :

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

If your property is set to retain then you must use self. to access it like this :

self.fechaLectura = [consumo objectForKey:@"fechaLectura"];
秋千易 2025-01-02 12:28:42

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

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

You need to assign the properties not the instance variables, to properly retain your values.

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

And in the dealloc method you need to release each one of those without the self.

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

    [super dealloc];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文