I've been having trouble adding items to a mutable array and getting those items to populate a UITableView. They currently populate just fine, but after I add one item I get "Unrecognized Selector Sent to Instance" on the [reloadData];
call. I've tried it in a number of different configurations but I cannot seem to get it to work. When I disable reloadData it adds multiple items to the array without issue, but the moment I bring it back is when I get the same error. This is the code that I'm working with:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (alertView.tag)
{
case 0:
_FiltrationString = [alertView textFieldAtIndex:0].text;
[_tankFilterArray addObject:_FiltrationString];
NSLog(@"Number of Filtration Parts Added: %lu", (unsigned long)_tankFilterArray.count);
break;
case 1:
_LightsString = [alertView textFieldAtIndex:0].text;
[_tankLightsArray addObject:_LightsString];
NSLog(@"Number of Lights Added: %lu", (unsigned long)_tankLightsArray.count);
break;
case 2:
_MovementString = [alertView textFieldAtIndex:0].text;
[_tankMovementArray addObject:_MovementString];
NSLog(@"Number of Water Movement Added: %lu", (unsigned long)_tankMovementArray.count);
break;
default:
break;
}
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
[_tankFiltration reloadData];
[_tankLights reloadData];
[_tankMovement reloadData];
}
ADDITIONAL CODE
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
switch (tableView.tag)
{
case 0:
_tankFiltration = [_tankFilterArray objectAtIndex:indexPath.row];
cell.textLabel.text = [_tankFilterArray objectAtIndex:indexPath.row];
break;
case 1:
_tankLights = [_tankLightsArray objectAtIndex:indexPath.row];
cell.textLabel.text = [_tankLightsArray objectAtIndex:indexPath.row];
break;
case 2:
_tankMovement = [_tankMovementArray objectAtIndex:indexPath.row];
cell.textLabel.text = [_tankMovementArray objectAtIndex:indexPath.row];
break;
case 3:
[self performSelector:@selector(saveButton)];
break;
default:
break;
}
cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == 0)
{
// MAKE FILTRATION TAG HERE
return _tankFilterArray.count;
}
if (tableView.tag == 1)
{
// MAKE LIGHTS TAG HERE
return _tankLightsArray.count;
}
if (tableView.tag == 2)
{
// MAKE MOVEMENT TAG HERE
return _tankMovementArray.count;
}
NSLog(@"Tank Movement Array Items: %lu", (unsigned long)_tankMovementArray.count);
NSLog(@"Tank Lights Array Items: %lu", (unsigned long)_tankLightsArray.count);
NSLog(@"Tank Filtration Array Items: %lu", (unsigned long)_tankFilterArray.count);
return _tankMovementArray.count;
return _tankLightsArray.count;
return _tankFilterArray.count;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
return self;
}
Does anyone have any idea what could be causing this? I've had a few people take a look at it on my side and no one can figure it out. I appreciate the help in advance as always. This issue is proving to be a cascading problem that permeates throughout the rest of my app and makes it difficult for testing.
Updated Edit
Fixed it!
The problem was in cellForRowAtIndexPath
. Specifically:
_tankFiltration = [_tankFilterArray objectAtIndex:indexPath.row];
Was redeclaring the UITableView as an array. I can't believe I didn't see it before. Thank you so much everyone!
If _FiltrationString is an NSString you should be using this command:
_FiltrationString = [NSString stringWithFormat:@"%@",[alertView textFieldAtIndex:0].text];
[_tankFilterArray addObject:_FiltrationString];
instead of
_FiltrationString = [alertView textFieldAtIndex:0].text;
The command equates _FiltrationString to the address of the alert view textfield.