0

在我的应用程序中,用户必须通过在表格视图中输入答案来回答给定的问题。我有一个表格视图,它以一个自定义单元格开头,用户在其中输入一些数据。该单元格有一个按钮,可以加载另一个自定义单元格,用户可以在其中输入更多数据。

一旦用户回答了问题,表格视图就会被修改,以便它只有第一个单元格用于新问题。此外,几个“数据”数组,包括leftData.equation负责跟踪用户输入的数组,都被清除了所有对象。( [array removeAllObjects])

加载第二个单元格或返回第一个单元格进行下一个问题时都没有崩溃。但是,当我再次尝试加载第二个单元格时,程序会因访问错误而崩溃。我假设它与数组有关,leftData.equation

下面是第二个单元格加载时调用的方法

- (void) setUpBalanceCell: (UITableView *) tableView  {

    balanceCell = (BalanceCell *) [tableView dequeueReusableCellWithIdentifier:@"balanceCell"];

    if (balanceCell == nil) {   

        NSArray *nib =   [[NSBundle mainBundle] loadNibNamed:@"BalanceCell" owner:self options:nil];
        balanceCell = (BalanceCell*) [nib objectAtIndex:0];
        [balanceCell.rightButton addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];

    }
        // stores data from first cell into a data array
        for (FormulaLabel *label in equationCell.leftView.equationOrder) 
            [leftData.equation addObject:label.text];   //fix:  needs to comply with MVC   

        for (FormulaLabel *label in equationCell.rightView.equationOrder)  
            [rightData.equation addObject:label.text];  

        [leftData setUpBalancedEquation];
        [rightData setUpBalancedEquation];

        [self setUpView:balanceCell.leftView fromArray:leftData.equation toArray:leftBalanceItems]; // WHERE EXC_BAD ACCESS appears
        [self setUpView:balanceCell.rightView fromArray:rightData.equation toArray:rightBalanceItems];

}

调用坏访问的方法

- (void)setUpView:(UIView *)view fromArray:(NSMutableArray *)equationData toArray:(NSMutableArray *)balanceItems {

int labelCount = 0; //label #
int startingPoint = 5; //x vaiue where first label starts

//Crashes in method below
for (NSString *equationText in equationData) {

    //add text
    FormulaLabel *tempLabel = [[FormulaLabel alloc] initWithFrame:CGRectMake(startingPoint, 2, 10, 22)];
    tempLabel.text = equationText;
    [tempLabel sizeToFit];
    [view addSubview:tempLabel];
    tempLabel.tag = labelCount;
    [balanceItems addObject:tempLabel];
    //set location of '+'
    startingPoint = tempLabel.frame.origin.x + tempLabel.frame.size.width + 3;

    if (labelCount != [equationData count]-1) { //not the last label

        UILabel *plus = [[[UILabel alloc] initWithFrame:CGRectMake(startingPoint, 5, 10, 10)]autorelease];
        plus.text = @"+";
        plus.font = [UIFont systemFontOfSize:13];
        [plus sizeToFit];
        [view addSubview:plus];
        startingPoint = plus.frame.origin.x + plus.frame.size.width + 3;
        [balanceItems addObject:plus];
    }

    labelCount ++;
    [tempLabel release];
}

}

4

1 回答 1

1

好吧,保留它肯定会解决它,但如果我是你,我会首先查看阵列的发布位置。

Calling retain increments the retain count of an object. When you first create an object with [NSObject alloc] the retain count is set to 1. calling retain increases the count so it becomes 2. calling release, on the other hand, decrements the retain count by 1. so, assume you call release on an object with retain count of 1, it becomes 0. When it is 0, the object is released from memory (and calls dealloc).

I hope this makes things clear, memory management is very confusing. You might want to look into testing for memory leaks later on in your project.

best of luck.

于 2012-07-02T05:25:55.583 回答