2014-10-05 55 views
0

当我单击一行中的按钮时,不同行中的按钮消失。为什么会发生这种情况?按钮单击后UIButton消失

我看了下面的问题和其中的所有其他问题,但没有真正回答我的问题。

我用调试Color Blended Layers,看它是否只是一种颜色的东西,但我的按钮只是似乎完全消失。我怀疑这是一个button.hidden属性的东西,所以我硬编码button.hidden = NO;但没有任何改变。

这里出了什么问题?

表控制代码:复选按钮被选中

Edit button is visible

后:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if ([locationObjectsArray count] > 0) 
    { 
     return [locationObjectsArray count]+1; 
    } 
    else 
     return 1; 
} 

// Populate the Table View with member names 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier= @"Cell"; 

    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    // Configure the Cell... 
    UIButton *selectButton = (UIButton *)[cell viewWithTag:1]; 
    UILabel *cityNamesText = (UILabel *)[cell viewWithTag:2]; 
    UIButton *editButton = (UIButton *)[cell viewWithTag:3]; 

    //NSLog(@"[locationObjectsArray count]: %lu", (unsigned long)[locationObjectsArray count]); 

    if (indexPath.row >= [locationObjectsArray count]) { 
     // locationObjectsArray count == 0; Empty Array 
     cityNamesText.text = @"Add New Location"; 

     NSLog(@"%ld: %@", (long)indexPath.row, @"Add New Location"); 
     editButton.hidden = NO; 
     [editButton setTitle:@"Add" forState:UIControlStateNormal]; 
     //[editButton setTitle:@"Add" forState:UIControlStateApplication]; 
     selectButton.hidden = YES; 
    } 
    else if ([locationObjectsArray count] > 0) { 
     LocationObject *locObject = [locationObjectsArray objectAtIndex:indexPath.row]; 
     NSLog(@"%ld: %@", (long)indexPath.row, [locObject getLocationName]); 
     cityNamesText.text = [locObject getLocationName]; 
     selectButton.hidden = NO; 
     editButton.hidden = NO; 
    } 

    // Assign button tags 
    selectButton.tag = indexPath.row; 
    editButton.tag = indexPath.row; 

    [selectButton addTarget:self action:@selector(selectButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [editButton addTarget:self action:@selector(editButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

    LocationObject *selectedLocationObject = [self loadLocationObjectWithKey:@"locObject"]; 
    // Set Selected Cell to different Color 
    if ([cityNamesText.text isEqualToString:[selectedLocationObject getCityName]]) { 
     // Change to lightBlue color 
     UIColor * lightBlue = [UIColor colorWithRed:242/255.0f green:255/255.0f blue:254/255.0f alpha:1.0f]; 
     [cell setBackgroundColor:lightBlue]; 
    } 
    else 
    { 
     // All non-selected cells are white 
     //[cell setBackgroundColor:[UIColor whiteColor]]; 
     //editButton.hidden = NO; 
    } 

    return cell; 
} 

// Select Button Clicked method 
-(void)selectButtonClicked:(UIButton*)sender 
{ 
    if ([locationObjectsArray count] == 0) 
    { 
     NSLog(@"locObject count == 0"); 
     // locationObjectsArray count == 0; Empty Array 
     // City name input is invalid 
     UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"No Locations Set" 
                 message:@"Please add a new location." 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles: nil]; 
     [alert show]; 
    } 
    else 
    { 
     NSLog(@"locObject count > 0"); 
     if (sender.tag >= locationObjectsArray.count) { 
      // Create local isntance of the selected locationObject 
      LocationObject *locObject = [locationObjectsArray objectAtIndex:sender.tag]; 
      // Set locObject as current default locObject 
      [self saveLocationObject:locObject key:@"locObject"]; 
     } 


     [mainTableView reloadData]; 
    } 
} 

// Edit Button Clicked method 
-(void)editButtonClicked:(UIButton*)sender 
{ 
    if ([locationObjectsArray count] == 0) { 
     // locationObjectsArray count == 0; Empty Array 
     UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Add Location" 
                 message:@"Input City Name" 
                 delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles: nil]; 
     alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
     [alert addButtonWithTitle:@"Save"]; 
     [alert show]; 
    } 
    else 
    { 
     selectedObjectInArray = sender.tag; 
     UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Edit Location" 
                 message:@"Input City Name" 
                 delegate:self 
               cancelButtonTitle:@"Cancel" 
               otherButtonTitles: nil]; 
     alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
     [alert addButtonWithTitle:@"Save"]; 
     [alert show]; 
    } 
} 

// Handle alertView 
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    if ([alertView.title isEqualToString:@"Add Location"]) { 
     // Add Location Alert View 
     if (buttonIndex == 0) 
     { 
      NSLog(@"You have clicked Cancel"); 
     } 
     else if(buttonIndex == 1) 
     { 
      NSLog(@"You have clicked Save"); 
      UITextField *cityNameTextField = [alertView textFieldAtIndex:0]; 
      NSString *saveLocationName = cityNameTextField.text; 
      NSLog(@"saveLocationName: %@", saveLocationName); 
      if ([self isLocationValid:saveLocationName] == YES) { 
       NSLog(@"location is valid. locationObjectsArray.count = %lu", locationObjectsArray.count); 
       if (locationObjectsArray.count == 0) { 
        locationObjectsArray = [NSMutableArray array]; 
       } 
       // City name input is valid 
       LocationObject *locObject = [[LocationObject alloc] init]; 
       [locObject setCityName:saveLocationName]; 
       locObject.byCityName = YES; 
       [locationObjectsArray addObject:locObject]; 

       NSLog(@"After addObject: locationObjectsArray.count = %lu", locationObjectsArray.count); 

       [self saveLocationArrayObject:locationObjectsArray key:@"locationObjectsArray"]; 
       [mainTableView reloadData]; 
      } 
      else 
      { 
       // City name input is invalid 
       UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"City Name Invalid" 
                   message:@"Unable to locate input city." 
                   delegate:self 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles: nil]; 
       [alert show]; 
      } 
     } 
    } 
    else if ([alertView.title isEqualToString:@"Edit Location"]) 
    { 
     // Edit Location Alert View 
     if (buttonIndex == 0) 
     { 
      NSLog(@"You have clicked Cancel"); 
     } 
     else if(buttonIndex == 1) 
     { 
      NSLog(@"You have clicked Save"); 
      UITextField *cityNameTextField = [alertView textFieldAtIndex:0]; 
      NSString *saveLocationName = cityNameTextField.text; 
      if ([self isLocationValid:saveLocationName]) { 
       // City name input is valid 
       int selectedIndex = (int)selectedObjectInArray; 
       LocationObject *locObject = [locationObjectsArray objectAtIndex:selectedIndex]; 
       [locObject setCityName:saveLocationName]; 
       [locObject setByCityName:(Boolean *)TRUE]; 
       [locationObjectsArray setObject:locObject atIndexedSubscript:selectedIndex]; 
       [self saveLocationArrayObject:locationObjectsArray key:@"locationObjectsArray"]; 
       [mainTableView reloadData]; 
      } 
      else 
      { 
       // City name input is invalid 
       UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"City Name Invalid" 
                   message:@"Unable to locate input city." 
                   delegate:self 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles: nil]; 
       [alert show]; 
      } 
     } 
    } 

} 

之前

Edit button is gone

+0

你能发布代码吗? – YogevSitton 2014-10-05 18:35:15

+0

确定使用tableView代码更新了@godmoney – erad 2014-10-05 18:37:43

+0

您可能会遇到使用标签的问题。通过查找带有标签1,2和3的视图,您可以获得对按钮的引用,但随后您会根据indexPath.row设置其中两个按钮的标签。通常情况下,你会做一个或另一个,但不是两个。就个人而言,我真的不喜欢根据他们的标签找到意见。我总是做一个自定义的单元类,并给我的观点IBOutlets。它更干净,并使您的代码更具可读性。 – rdelmar 2014-10-05 18:51:12

回答

2

你的问题是,在这些线路中cellForRowAtIndexPath

// Assign button tags 
selectButton.tag = indexPath.row; 
editButton.tag = indexPath.row; 

随着单元格被重用,标签会混合起来,我建议在这种情况下尝试省略标签并使用例如作为@rdelmar的IBOutlets指出。