In my app, the user must answer a given question by inputting the answer within a table view. I have a table view that starts with one custom cell, in which the user inputs some data. That cell has a button that can load another custom cell, in which the user inputs some more data.
Once the user answers the question, the table view is modified so that it has only the first cell, for the new question. Also, several "data" arrays, including leftData.equation
that are responsible for keeping track of the user's input are cleared of all objects. ( [array removeAllObjects]
)
There is no crash when the second cell is loaded nor when I return to the first cell for the next question. However, when I try loading the second cell again, the program crashes with Bad Access. I'm assuming it has something to do with the array, leftData.equation
Below is the method called when the second cell loads
- (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];
}
The method where bad access is called
- (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];
}
}