一旦UITableView重新加载,应用程序就会崩溃

时间:2011-01-10 04:15:39

标签: iphone cocoa-touch uitableview

我正在开发一个应用程序,一旦登录过程完成,TableView就需要重新加载。重新加载表数据时,应用程序崩溃并显示错误EXC_BAD_ACCESS。删除除case 0:

之外的所有案例实例时,它不会崩溃

背后的原因是什么?

以下是代码:

- (void)viewDidLoad {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(loginDone:) 
                                                     name:@"loginDone" object:nil];
    statTableView.backgroundColor = [UIColor clearColor];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)
section  {

return 6;
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell.

    switch (indexPath.row) {

        case 0 :

            cell.textLabel.text = @"Foo:";
            NSLog(@"%@", data7);
            UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
            myLabel2.text = (@"%@", data7);
            myLabel2.textColor = [UIColor blackColor];
            myLabel2.backgroundColor = [UIColor whiteColor];
            myLabel2.font = [UIFont fontWithName:@"Trebuchet MS" size:14];
            [cell.contentView addSubview:myLabel2];
            break;

        case 1: 

            cell.textLabel.text = @"Foo: ";

            UILabel *myLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
            myLabel4.text = (@"%@", data11);
            myLabel4.textColor = [UIColor blackColor];
            myLabel4.backgroundColor = [UIColor whiteColor];
            myLabel4.font = [UIFont fontWithName:@"Trebuchet MS" size:14];
            [cell.contentView addSubview:myLabel4];
            break;

        case 2:

            cell.textLabel.text = @"Foo: ";

            UILabel *myLabel8 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
            myLabel8.text = (@"%@", data3);
            myLabel8.textColor = [UIColor blackColor];
            myLabel8.backgroundColor = [UIColor whiteColor];
            myLabel8.font = [UIFont fontWithName:@"Trebuchet MS" size:14];
            [cell.contentView addSubview:myLabel8];
            break;

        case 3:

            cell.textLabel.text = @"Foo: ";


            UILabel *myLabel10 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
            myLabel10.text = [NSString stringWithFormat:@"%@", data4];

            if ([data4 isEqualToString:@"0"]) {
                myLabel10.text = @"None";
            }   

            myLabel10.textColor = [UIColor blackColor];
            myLabel10.backgroundColor = [UIColor whiteColor];
            myLabel10.font = [UIFont fontWithName:@"Trebuchet MS" size:14];
            [cell.contentView addSubview:myLabel10];
            break;

        case 4:

            cell.textLabel.text = @"Foo: ";


            UILabel *myLabel12 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
            myLabel12.text = [NSString stringWithFormat:@"%@", data5];
            myLabel12.textColor = [UIColor blackColor];
            if ([data5 isEqualToString:@"Foo"]) {
                myLabel12.textColor = [UIColor redColor];
                myLabel12.text = @"Nil";
            } 
            myLabel12.backgroundColor = [UIColor whiteColor];
            myLabel12.font = [UIFont fontWithName:@"Trebuchet MS" size:14];
            [cell.contentView addSubview:myLabel12];
            break;

        case 5:

            cell.textLabel.text = @"Foo: ";


            UILabel *myLabel14 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 30)];
            if ([data6 isEqualToString:@"Foo"]) {
                myLabel14.textColor = [UIColor colorWithRed:(0/255.f) green:(100/255.f) blue:(0/255.f) alpha:1.0];
                myLabel14.text = @"No Dues";
            } else {
                myLabel14.text = [NSString stringWithFormat:@"%@", data6];
                myLabel14.textColor = [UIColor redColor];
            }
            myLabel14.backgroundColor = [UIColor whiteColor];
            myLabel14.font = [UIFont fontWithName:@"Trebuchet MS" size:14];
            [cell.contentView addSubview:myLabel14];
            break;

            /*
             [myLabel2 release];
             [myLabel4 release];
             [myLabel8 release];
             [myLabel10 release];
             [myLabel12 release];
             [myLabel14 release];
                    */
    }

    return cell;    
}

3 个答案:

答案 0 :(得分:2)

您需要在此处进行一些基本调试。首先添加断点并在调试器中逐行进行,直到找到发生错误exec的行。一旦你有了,你将能够快速找出原因。

答案 1 :(得分:0)

dataX变量是什么?如果它们被释放并且未设置为nil,那么您将在解除分配的对象上调用方法,这将导致EXC_BAD_ACCESS。

答案 2 :(得分:0)

您的代码存在一些问题。

首先,以下方法需要自动释放UITableViewCell对象。

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

其次,单元格可能来自 [tableView dequeueReusableCellWithIdentifier:CellIdentifier] ,它已经有一个你添加的自定义标签。并且您将为此类单元格添加更多自定义标签。您可以对此类单元格进行子类化,并按属性获取所需的UILabel。

相关问题