【问题标题】:UIButton disappearing after button click按钮单击后UIButton消失
【发布时间】:2014-10-06 02:58:55
【问题描述】:

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

我查看了以下问题以及其中的所有其他问题,但没有任何内容能真正回答我的问题。

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

这里出了什么问题?

表格控制代码:

- (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];
            }
        }
    }

}

之前:

选中检查按钮后:

【问题讨论】:

  • 能贴出代码吗?
  • OK 更新了tableView代码@godmoney
  • 您使用标签的方式可能有问题。您可以通过查找带有标签 1、2 和 3 的视图来获得对按钮的引用,但稍后您会根据 indexPath.row 设置其中两个按钮的标签。通常,你会做其中的一个或另一个,但不会两者都做。就个人而言,我真的不喜欢根据他们的标签查找视图。我总是制作一个自定义单元类,并给出我的意见 IBOutlets。它更干净,并使您的代码更具可读性。
  • 已经使用自定义子类,viewWithTag: 仅对简单视图有用。

标签: ios objective-c


【解决方案1】:

您的问题在cellForRowAtIndexPath 中的这些行中:

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

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

【讨论】:

    猜你喜欢
    • 2017-03-03
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2014-08-19
    • 2021-09-12
    • 2018-01-06
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多