私のアプリでは、ユーザーは表ビュー内に回答を入力して、特定の質問に答える必要があります。ユーザーがデータを入力する 1 つのカスタム セルで始まるテーブル ビューがあります。そのセルには、ユーザーがさらにデータを入力する別のカスタム セルを読み込むことができるボタンがあります。
ユーザーが質問に答えると、テーブル ビューが変更され、新しい質問の最初のセルのみが表示されます。leftData.equation
また、ユーザーの入力の追跡を担当するものを含む、いくつかの「データ」配列は、すべてのオブジェクトからクリアされます。( [array removeAllObjects]
)
2 番目のセルがロードされたときも、次の質問のために最初のセルに戻ったときも、クラッシュはありません。しかし、2 番目のセルを再度ロードしようとすると、プログラムが Bad Access でクラッシュします。配列と関係があると思いますが、leftData.equation
以下は、2 番目のセルが読み込まれたときに呼び出されるメソッドです。
- (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];
}
}