当我单击一行中的按钮时,另一行中的按钮消失。为什么会发生这种情况?
我看了下面的问题以及其中的所有其他问题,但没有真正回答我的问题。
我使用调试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];
}
}
}
}
在:
选中检查按钮后:
答案 0 :(得分:2)
您的问题出现在cellForRowAtIndexPath
:
// Assign button tags
selectButton.tag = indexPath.row;
editButton.tag = indexPath.row;
随着细胞的重复使用,标签会混淆,我建议在这种情况下尝试省略标签并使用例如作为@rdelmar的IBOutlets指出。