2012-06-20 95 views
3

首先關閉標籤將不起作用。我這樣說是因爲我創建了4個按鈕,並且對於特定的單元格使用相同的標記,例如indexPath.row = tag。如何檢查兩個對象(UIButtons)是否相同

裏面我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; 
     } 

這裏就是我試圖訪問我在小區創建的單選按鈕。我想這樣做是這樣的

如果([學分objectAtIndex:1])== radioButtonA {

做的東西。原因im不說== [radioButtonA標籤]是因爲我有三個其他按鈕都具有相同的標籤。如果你閱讀了代碼,你就會明白爲什麼這些標籤是這樣設置的。 }

我要求的是1幫助,2有另一種方法來檢查兩個按鈕即對象是否相等,而不必依賴於他們的標籤。

不要擔心嘗試捕捉最後我用它來趕上拋出的異常。

 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(@ 「%@」,[幣objectAtIndex:ⅰ]); NSLog(@「%@」,radioButtonA);

 THE First output is the log of the [credits object atIndex:i] 

UIButton:0x6c91430; frame =(86 110; 32 30); opaque = NO;層= CALayer:0x6c914f0

2012-06-20 20:24:01.568 TableView [12557:f803] UIButton:0x6ea8ad0; frame =(86 110; 32 30); opaque = NO; tag = 6;層= CALayer的:0x6e746e0

正如你所看到的UIBUttons是不同的。因此==操作符不工作

+0

==應該正常工作。你有錯誤嗎?如果是這樣,錯誤是什麼?堆棧跟蹤? – sosborn

+0

與上述評論商定,具體而言,''==應用於對象測試地址是否相等,從而,如果它是由一個不同的變量引用的同一個對象,它會在那個一個地址。 –

+0

這裏是日誌... NSLog(@「%@」,[credits objectAtIndex:i]); NSLog(@「%@」,radioButtonA);第一個輸出是[信貸對象atIndex:i]的日誌2012-06-20 20:22:14。219 TableView [12536:f803] >這第二個日誌是radioButtonA的日誌2012-06-20 20:22:14.220 TableView [12536:f803] > – KING

回答

0

我不知道,在方法, - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 你應該添加按鈕,只有當細胞這樣

- (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; 
} 
+0

這導致單元格加載多次... – KING

+1

如何使用自定義單元格? – Siam

5

當檢查,對一二對象另一個你不能使用==,你必須使用[objectA isEqual:objectB],如果這兩個對象是一樣的答案是YES和否,如果他們不是。

要了解更多,請訪問:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html

,並檢查什麼是書面的isEqual:

+0

對象[對象A的isEqual:對象B]將永遠不會在我的程序等於...當點擊了按鈕被髮送者被放置到一個數組,並且發送器(UIButton的)正被改變每次程序運行。我不會發布這個問題,如果這是我已經嘗試了所有我能想到的解決方案的解決方案,所以我張貼在這裏接受幫助。謝謝,即使你的方法不起作用。 – KING

+0

正確的!另外,如果你是比較同類型的對象,你可以使用這些更專業的功能:「isEqualToString:','isEqualToArray'等 – pasawaya

+1

==應該只是罰款爲指針... – WendiKidd

0

我已經知道這個nil.just,然而,這篇文章的目的是爲了看看他們是任何其他方式。我還使用了isMemberOfClass:[UIButton class]來進一步縮小範圍。最好的方法和最有效的方法是使用對象的標籤進行比較。

相關問題