滚动视图时为EXC_BAD_ACCESS [英] EXC_BAD_ACCESS when I scroll my view

查看:78
本文介绍了滚动视图时为EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种观点:第一种是朴素的UITableView(以编程方式创建).第二个是UIScrollView,其中包含图像和成组样式的UITableView(也是通过编程方式创建的).

I have two views: the first one is a UITableView in plain style (created programmatically); the second one is a UIScrollView that contains an image and a UITableView in grouped style (also created programmatically).

当我从firstView-> secondView导航时,一切正常.但是,如果我回到firstView,然后尝试第二次导航firstView-> secondView,则会收到EXC_BAD_ACCESS错误.

When I navigate from the firstView -> secondView everything works fine. However if I come back to the firstView and then I try to navigate firstView ->secondView for the second time, I get a EXC_BAD_ACCESS error.

更新

这是secondView中的UITableView委托的代码:

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

    switch (section) {
        case 0: 

        //header - breve descrizione
        return 2;

        break;
    case 1: //mappa

        //header - mappa statica (immagine) - footer (indirizzo)
        return 3;

        break;
    case 2: //review

        //header - prima review
        return 2;

        break;
    default:
        return 0;

        break;


 }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0: //case titolo (header)


    return HTABLE_DESCR_HEADER;
                    break;

                case 1: //case corpo
                    return HTABLE_DESCR_BODY;
                    break;

                default:
                    return 0;
                    break;
            }
            break;
        case 1:

            switch (indexPath.row) {
                case 0: //case titolo (header)
                    return HTABLE_LOC_HEADER;
                    break;

                case 1: //case corpo
                    return HTABLE_LOC_BODY;
                    break;

                case 2: //case footer 
                    return HTABLE_LOC_FOOTER;
                    break;

                default:
                    return 0;
                    break;
            }

            break;
        case 2:

            switch (indexPath.row) {
                case 0: //case titolo (header)
                    return HTABLE_REV_HEADER;
                    break;

                case 1: //case corpo
                    return HTABLE_REV_BODY;
                    break;

                default:
                    return 0;
                    break;
            }

            break;
        default:            
            return 0;
            break;
    }

}

/*- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

 return @"Travellers Guide";

 }*/

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    int section = 2;

    //se ci sono review
    if ([[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:@"list_review"] count] > 0) {
        section++;
    }

    return section;

}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [self getCellContentView:CellIdentifier];
    }    

    cell.accessoryType = UITableViewCellAccessoryNone;

    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    switch (indexPath.section) {
        case 0: //descrizione

            switch (indexPath.row) {
                case 0: //header

                    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"language"] isEqualToString:@"it"]) {
                        cell.textLabel.text = @"Descrizione";
                    } else {
                        cell.textLabel.text = @"Description";
                    }
                    cell.textLabel.backgroundColor = [UIColor clearColor];
                    cell.textLabel.textColor = [UIColor blackColor];
                    cell.textLabel.shadowColor = [UIColor whiteColor];
                    cell.textLabel.shadowOffset = CGSizeMake(1, 1);
                    cell.textLabel.highlightedTextColor = [UIColor blackColor];
                    cell.textLabel.font = [UIFont boldSystemFontOfSize:11.0];
                    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"rigaDescrHeader.png"]];

                    break;

                case 1: //descrizione

                    NSLog(@"Descr");

                    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"rigaRevBody.png"]];
                    cell.textLabel.text = [[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:@"description"];
                    cell.textLabel.font = [UIFont systemFontOfSize:10.0];
                    cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
                    cell.textLabel.numberOfLines = 3;
                    cell.textLabel.textColor = [UIColor blackColor];
                    cell.textLabel.backgroundColor = [UIColor clearColor];
                    cell.textLabel.shadowColor = [UIColor whiteColor];
                    cell.textLabel.shadowOffset =CGSizeMake(1, 1);
                    cell.textLabel.highlightedTextColor = [UIColor blackColor];

                    break;

                default:
                    break;
            }

            break;
        case 1: //mappa

            //header - mappa statica (immagine) - footer (indirizzo)
            switch (indexPath.row) {
                case 0: //header

                    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"language"] isEqualToString:@"it"]) {
                        cell.textLabel.text = @"Posizione";
                    } else {
                        cell.textLabel.text = @"Location";
                    }
                    cell.textLabel.font = [UIFont boldSystemFontOfSize:11.0];
                    cell.textLabel.backgroundColor = [UIColor clearColor];
                    cell.textLabel.textColor = [UIColor blackColor];
                    cell.textLabel.shadowColor = [UIColor whiteColor];
                    cell.textLabel.shadowOffset = CGSizeMake(1, 1);
                    cell.textLabel.highlightedTextColor = [UIColor blackColor];
                    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"rigaLocHeader.png"]];

                    break;

                case 1: //mappa

                    NSLog(@"Mappa");

                    CLLocationDegrees latitude  = [[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:@"latitude"] doubleValue];
                    CLLocationDegrees longitude = [[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:@"longitude"] doubleValue];
                    CLLocation* poiLocation = [[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease];
                    Annotation *ann = [Annotation annotationWithCoordinate:poiLocation.coordinate];
                    //mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, HTABLE_LOC_BODY)];
                    [mapView setHidden:NO];
                    [mapView addAnnotation:ann];
                    MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
                    region.center = poiLocation.coordinate;                    
                    region.span.longitudeDelta = 0.05f;
                    region.span.latitudeDelta  = 0.05f;
                    [self.mapView setRegion:region animated:YES];
                    [self.mapView regionThatFits:region];
                    cell.backgroundView = mapView;


                    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 302, 120)]; 
                    imgView.image = [UIImage imageNamed:@"ombraMappa.png"];
                    [cell.backgroundView addSubview:imgView];
                    [imgView release];

                    break;

                case 2: //footer

                    cell.textLabel.text = [[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:@"locality"];
                    cell.textLabel.font =[UIFont systemFontOfSize:10.0];
                    cell.textLabel.backgroundColor = [UIColor clearColor];
                    cell.textLabel.textColor = [UIColor blackColor];
                    cell.textLabel.shadowColor = [UIColor whiteColor];
                    cell.textLabel.shadowOffset = CGSizeMake(1, 1);
                    cell.textLabel.highlightedTextColor = [UIColor blackColor];

                    cell.detailTextLabel.text = [[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:@"address"];
                    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:9.0];
                    cell.detailTextLabel.textColor = [UIColor grayColor];
                    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
                    cell.detailTextLabel.shadowColor = [UIColor whiteColor];
                    cell.detailTextLabel.shadowOffset = CGSizeMake(1, 1);
                    cell.detailTextLabel.highlightedTextColor = [UIColor blackColor];
                    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"rigaLocFooter.png"]];

                    break;

                default:
                    break;
            }

            break;
        case 2: //review

            //header - mappa statica (immagine) - footer (indirizzo)
            switch (indexPath.row) {
                case 0: //header

                    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"language"] isEqualToString:@"it"]) {
                        cell.textLabel.text = [NSString stringWithFormat:@"Recensioni (%d)",[[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:@"list_review"] count]];
                    } else {
                        cell.textLabel.text = [NSString stringWithFormat:@"Review (%d)",[[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:@"list_review"] count]];
                    }
                    cell.textLabel.font = [UIFont boldSystemFontOfSize:11.0];
                    cell.textLabel.backgroundColor = [UIColor clearColor];
                    cell.textLabel.textColor = [UIColor blackColor];
                    cell.textLabel.shadowColor = [UIColor whiteColor];
                    cell.textLabel.shadowOffset = CGSizeMake(1, 1);
                    cell.textLabel.highlightedTextColor = [UIColor blackColor];

                    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"rigaLocHeader.png"]];

                    break;

                case 1: //review

                    cell.textLabel.text = [[[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:@"list_review"] objectAtIndex:0] objectForKey:@"review"];
                    cell.textLabel.font =[UIFont systemFontOfSize:10.0];
                    cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
                    cell.textLabel.numberOfLines = 3;
                    cell.textLabel.backgroundColor = [UIColor clearColor];
                    cell.textLabel.textColor = [UIColor blackColor];
                    cell.textLabel.shadowColor = [UIColor whiteColor];
                    cell.textLabel.shadowOffset = CGSizeMake(1, 1);
                    cell.textLabel.highlightedTextColor = [UIColor blackColor];
                    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"rigaRevBody.png"]];

                    break;

                default:
                    break;
            }

            break;
        default:                
            break;
    }

    return cell;

}


- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

    return cell;
}

推荐答案

EXC_BAD_ACCESS并非由一个视图到另一个视图的进展引起.相反,应该归咎于这些视图的属性,或者视图对象本身的创建和发布(或不发布).

The EXC_BAD_ACCESS isn't caused by the progression of one view to the next. Rather, it will be properties of those views, or the creation and release - or not - of the view objects themselves that are to blame.

您的第一步是运行分析器.然后,如果您已解决所有问题,但仍然存在问题,请开始在Instruments中运行Leaks工具.

Your first step is to run the Analyzer. Then, if you've fixed all that and theres still a problem, start running the Leaks tool in Instruments.

有关解决EXC_BAD_ACCESS问题的更多详细信息,以及有关导致这些错误的原因的出色链接以及有关如何解决的逐步说明,请查看以下问题的答案:

For more details on resolving EXC_BAD_ACCESS issues, along with the excellent link on what causes these errors and step by step instructions for how to fix, have a look at answers to these questions:

在Xcode4中找到EXC_BAD_ACCESS的原因

随机输入EXC_BAD_ACCESS一个不可能发生的地方

在您的情况下,我将专门研究您如何创建secondView以及返回secondView时对firstView的处理方式.

In your case, I'd specifically look at how you are creating secondView, and what you do with firstView when you return to secondView.

这篇关于滚动视图时为EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆