gpt4 book ai didi

ios - 单击按钮后 UIButton 消失

转载 作者:行者123 更新时间:2023-11-28 21:57:01 25 4
gpt4 key购买 nike

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

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

我使用调试 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];
}
}
}

}

之前:

Edit button is visible

选中复选按钮后:

Edit button is gone

最佳答案

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

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

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

关于ios - 单击按钮后 UIButton 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26205847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com