开发者

App is crashing as soon as UITableView gets reloaded

I'm developing an app where TableView needs to reload as soon as the login process gets completed. The app crashes with error EXC_BAD_ACCESS when the table data gets reloaded. It doesn't crash when I remove all case instances except case 0:

What could be the reason behind it?

Here's the code:

- (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 = [NSStrin开发者_开发技巧g 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;    
}


You need to do some basic debugging here. First add break points and go line by line in the debugger until you find the line where the bad exec is happening. Once you have that you will be able to quickly figure out why.


Your codes have some problems.

First, the following method needs an autorelease UITableViewCell object.

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

Second, the cell maybe come from [tableView dequeueReusableCellWithIdentifier:CellIdentifier], it already has a custom label which you has added. And you will add more custom label to this kind cell. You can sub-class this kind cell, and obtain the desired UILabel by property.


What are the dataX variables? If they are getting released and not set to nil, you will be calling methods on deallocated objects, which would cause EXC_BAD_ACCESS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜