まずタグが機能しません。これは、特定のセル、つまり indexPath.row = tag に対してすべて同じタグを持つ 4 つのボタンを作成するためです。
私のTableViewCellForRowAtIndexpathの中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"courseCell";
//Step 1: Check to se if we can reuse a cell from a row that has just rolled off the screen
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//step 2: If there are no cell to reuse, create a new one
if (cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
...
//-------------Creation of Custom Buttons-------------//
//-----img = "radioOn.png"-----//
//----img2 = "radioOff.png"----//
//----RadioButtonA----//
...
radioButtonA = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonA addTarget:self action:@selector(radioButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
radioButtonA.tag=indexPath.row;
//----End RadioButtonA----//
//----RadioButtonB----//
radioButtonB = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonB addTarget:self action:@selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
radioButtonB.tag =indexPath.row;
...
//----End RadioButtonB----//
//----RadioButtonC----//
radioButtonC = [UIButton buttonWithType:UIButtonTypeCustom];
[radioButtonC addTarget:self action:@selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
radioButtonC.tag = indexPath.row;
...
//----End RadioButtonC----//
//----RadioButtonNA----//
radioButtonNA = [UIButton buttonWithType:UIButtonTypeCustom];
radioButtonNA.tag = indexPath.row;
[radioButtonNA addTarget:self action:@selector(radioButtonClicked:)forControlEvents:UIControlEventTouchUpInside];
...
//----End RadioButtonC----//
//---------End of Radio Button Creations---------//
//---------UIStepper & StepperLabel Creation-----//
[cell.contentView addSubview:radioButtonA];
[cell.contentView addSubview:radioButtonB];
[cell.contentView addSubview:radioButtonC];
[cell.contentView addSubview:radioButtonNA];
//Step4: Return the cell
return cell;
}
#pragma mark - Buttons
- (void)radioButtonClicked:(UIButton *)sender
{
UIButton *myButton = sender;
// This Method and all the ones similar to this method is created to handle the UITouchUpInsideEvent that the user sends when pressing the radioButtons A-NA.
[radioButtons addObject:sender];
// Create an instance(object) of class NSIndexPath called indexPath and set its value the indexPath of the cell the user is currently in.
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *indexPath = [myTableView indexPathForCell:cell];
// Initialize two unique variables in order to check if the buttons being clicked are being referenced in the correct index.
int row = indexPath.row;
NSLog(@"Button is in row %d", row);
...
}
-(IBAction)button:(UIButton*)sender{
...
@try {
for (i=0; i<8; i++) {
if ([credits count ] ==0) {
break;
}
HEREは、セルで作成したラジオボタンにアクセスしようとしている場所です。私がやりたいことはこれです
if([credits objectAtIndex:i]) == radioButtonA{
何かをする。== [radioButtonA tag] と言っていない理由は、同じタグを持つ他の 3 つのボタンがあるためです。コードを読めば、タグがこのように設定されている理由がわかります。}
私が求めているのは 1 つのヘルプです。2 つのボタン、つまりオブジェクトがタグに依存せずに等しいかどうかを確認する別の方法があります。
Try catch 最終的にスローされた例外をキャッチするために使用していたことを心配しないでください。
if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:@"A"]) {
NSLog(@"radioA is in array");
creditHours+=[[valueArray objectAtIndex:i]doubleValue];
gradeEarned+=(GradeA.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
NSLog(@"%f",gradeEarned);
continue;
}
if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:@"B"]) {
NSLog(@"radioB is in array");
creditHours+=[[valueArray objectAtIndex:i]doubleValue];
gradeEarned+=(GradeB.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
continue;
}
if ([[[[credits objectAtIndex:i]titleLabel]text] isEqualToString:@"C"]){
NSLog(@"radioC is in array");
creditHours+=[[valueArray objectAtIndex:i]doubleValue];
gradeEarned+=(GradeC.doubleValue *[[valueArray objectAtIndex:i]doubleValue]);
continue;
}
if([credits objectAtIndex:i]== radioButtonNA){
NSLog(@"boboboobbobob");
continue;
}
}
}
@catch (NSException *exception) {
NSLog(@"NSException Caught");
NSLog(@"Name: %@",exception.name);
NSLog(@"Reason: %@", exception.reason);
}
@finally {
NSLog(@"in finally block");
}
// if ([credits objectAtIndex: i] == defaulter) {
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Failed to select all grades" message:[NSString stringWithFormat:@"Your grade selections have been reset"] delegate:self cancelButtonTitle:@"great"otherButtonTitles:nil];
// [alert show];
// [alert release];
// [self refreshArray];
// }
NSLog(@"%f",gradeEarned);
if (gradeEarned == 0) {
textLabel.text= [NSString stringWithFormat:@"%f",gradeEarned];
}else {
NSLog( @"boob");
sum= (gradeEarned)/(creditHours);
NSLog(@"%f",sum);
textLabel.text= [NSString stringWithFormat:@"%f",sum];
//[self refreshArray];
}
}
詳細について ログは次のとおりです...
NSLog(@"%@",[credits objectAtIndex:i]); NSLog(@"%@",radioButtonA);
THE First output is the log of the [credits object atIndex:i]
UI ボタン: 0x6c91430; フレーム = (86 110; 32 30); 不透明 = いいえ; レイヤー = CALayer: 0x6c914f0
2012-06-20 20:24:01.568 TableView[12557:f803] UIButton: 0x6ea8ad0; フレーム = (86 110; 32 30); 不透明 = いいえ; タグ = 6; レイヤー = CALayer: 0x6e746e0
ご覧のとおり、UIBUttons は異なるため、== 演算子は機能しません