简体   繁体   中英

error when add uitableviewcontroller as subview in uiviewcontroller

i'm doing a simple app that views items from a json server

I used UISegment in one UIViewController to add different subview

    - (IBAction)segmentSwitch:(id)sender {
    UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
    NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

    if (selectedSegment == 0) {
        instantiateViewControllerWithIdentifier:@"prestige"] animated:NO];

        UIViewController *subController = [self.storyboard instantiateViewControllerWithIdentifier:@"prestige"];
        [mainView addSubview:subController.view];

    }
    else if(selectedSegment == 1)
    {
        instantiateViewControllerWithIdentifier:@"latest"]];
        //[self setView: [self.storyboard instantiateViewControllerWithIdentifier:@"latest"]];
        //UIViewController *subController = [self.storyboard instantiateViewControllerWithIdentifier:@"latest"];
        //[mainView addSubview:subController.view];

        UITableViewController *tableVC =[self.storyboard instantiateViewControllerWithIdentifier:@"latest"];

        [self.view addSubview:tableVC.tableView];

    }
    else if (selectedSegment == 2)
    {
        instantiateViewControllerWithIdentifier:@"contactUs"]];
        UIViewController *subController = [self.storyboard instantiateViewControllerWithIdentifier:@"contactUs"];
        [mainView addSubview:subController.view];
    }
}

my error comes when I select select Segment 2 to view the uitableviewcontroller as subview x code give me the following error

exc_bad_access (code=1 address=0x110ff210 on line NSDictionary *latests = [latestArray objectAtIndex:indexPath.row];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"latestCell1";

    latestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSDictionary *latests = [latestArray objectAtIndex:indexPath.row];

    NSString *latest_name_En = [latests objectForKey:@"title_en"];
    NSString *logo1 = [latests objectForKey:@"logo"];
    NSString *img1 = [latests objectForKey:@"img1"];

    NSData *latestsImg1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:img1]];
    NSData *latestsLogo1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:logo1]];    

    cell.latest_name_en1.text = latest_name_En;

    dispatch_async(dispatch_get_main_queue(), ^{

        cell.latest_img1.image = [UIImage imageWithData:latestsImg1];
        cell.latest_logo1.image = [UIImage imageWithData:latestsLogo1];

    });

    return cell;
}

I'm sure the UITableViewController is working fine as I tried to run it alone and it work also tried it without custom cell and it worked also but with custon cell as a subview it gives the error above.

thanks for the help

I finally figure it out I used

[self addChildViewController:subController];
[mainView addSubview:subController.view];

to add the tableview to the viewcontroller as subview

and it worked

latestArray has not be initialized correctly.

Any time you get EXEC_BAD_ACCESS, run your code through the Zombies Instrument. This will point to any problematic code.

The objects latestsImg1 and latestsLogo1 are not being retained when they are initialized. Therefore, when the async code block executed within the dispatch_async is run those values are probably already released and no longer valid. So, when the async block executes those two pointers are point off into oblivion. I suggest either not doing those two assignments via GrandCentral dispatch, or if you must do them asynchronously, then retain them after creation and release them within the async block.

Something like:

NSData *latestsImg1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:img1]]; [latestsImg1 retain]; NSData *latestsLogo1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:logo1]];
[latestsLogo1 retain]; cell.latest_name_en1.text = latest_name_En;

dispatch_async(dispatch_get_main_queue(), ^{

    cell.latest_img1.image = [UIImage imageWithData:latestsImg1];

cell.latest_logo1.image = [UIImage imageWithData:latestsLogo1];
[latestsImg1 release];
    [latestLogo1 release];

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM