Reputation: 3088
I'm building a tableView that has three sections. I've got the first two working but the last one is a bit resistant. My problem seems to involve trying to declare a variable inside a switch statement, actually a nested switch statement. From what I've read that's not a good idea but in this case it seems like the only option.
The section in question dynamically accommodates the number of Alert objects that are associated with a particular piece of equipment. The Alerts are coming from Core Data and in place of "Date" and "Alert Message" I want to display the info from the Alert. I'm retrieving the relevant alerts using a NSFetchRequest. That returns an array of Alert objects that are sorted the way I want them. To display the proper information in the cellForRowAtIndexPath I've been trying to pull back that correct alert for the row using
Alert *alert = [allAlerts objectAtIndex:indexPath.row];
It seems I'm not allowed to declare a variable inside a switch statement. Any ideas how I can get around this? I'm going to have to do something similar in didSelectRowForIndexPath because I need to push a detail view when the cell is selected.
I've tried declaring the variable outside the switch statement but that's not going to work because the index.row can ask for an object at an index that doesn't exist in the array of Alert and that crashes the program.
The code in question is at the bottom of this method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Cell Identifiers
static NSString *attributeCellIdentifier = @"attributeCellIdentifier";
static NSString *operatingInfoCellIdentifier = @"operatingInfoCellIdentifier";
static NSString *alertCellIdentifier = @"alertCellIdentifier";
//Create Attribute Cell If Required
UITableViewCell *attributeCell = [tableView dequeueReusableCellWithIdentifier:attributeCellIdentifier];
if (attributeCell == nil) {
attributeCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:attributeCellIdentifier] autorelease];
attributeCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//Create Operating Info Cell If Required
UITableViewCell *operatingInfoCell = [tableView dequeueReusableCellWithIdentifier:operatingInfoCellIdentifier];
if (operatingInfoCell == nil) {
operatingInfoCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:operatingInfoCellIdentifier] autorelease];
operatingInfoCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//Create Alert Cell
UITableViewCell *alertCell = [tableView dequeueReusableCellWithIdentifier:alertCellIdentifier];
if (alertCell == nil) {
alertCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:alertCellIdentifier] autorelease];
alertCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
switch (indexPath.section) {
//Attribute Section
case 0:
switch (indexPath.row) {
case kEquipmentAttributeSectionNameRow:
attributeCell.textLabel.text = @"Name";
attributeCell.textLabel.textAlignment = UITextAlignmentRight;
attributeCell.selectionStyle = UITableViewCellSelectionStyleNone;
[attributeCell.contentView addSubview: self.nameField];
break;
case kEquipmentAttributeSectionLocationRow:
attributeCell.textLabel.text = @"Location";
attributeCell.textLabel.textAlignment = UITextAlignmentRight;
attributeCell.selectionStyle = UITableViewCellSelectionStyleNone;
[attributeCell.contentView addSubview: self.locationField];
break;
case kEquipmentAttributeSectionControllerSNRow:
attributeCell.textLabel.text = @"Serial #";
attributeCell.textLabel.textAlignment = UITextAlignmentRight;
attributeCell.selectionStyle = UITableViewCellSelectionStyleNone;
[attributeCell.contentView addSubview: self.controllerSNField];
break;
case kEquipmentAttributeSectionEquipTypeRow:
attributeCell.textLabel.text = @"EquipType";
attributeCell.textLabel.textAlignment = UITextAlignmentRight;
attributeCell.selectionStyle = UITableViewCellSelectionStyleNone;
[attributeCell.contentView addSubview: self.equipTypeField];
break;
return attributeCell;
}
break;
//Operating Info Section
case 1:
//Grab First Item in Event Array
switch (indexPath.row) {
//Event *recentEvent = [allEvents objectAtIndex:0];
//Last Update Row
case 0:
//Event *recentEvent = [allEvents objectAtIndex:0];
operatingInfoCell.textLabel.numberOfLines = 0;
operatingInfoCell.textLabel.textAlignment = UITextAlignmentCenter;
operatingInfoCell.textLabel.text = @"Last Update";
//operatingInfoCell.detailTextLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
operatingInfoCell.detailTextLabel.text = recentEvent.date;
return operatingInfoCell;
break;
//AvgSpeed Row
case 1:
operatingInfoCell.textLabel.numberOfLines = 0;
operatingInfoCell.textLabel.textAlignment = UITextAlignmentCenter;
operatingInfoCell.textLabel.text = @"Avg Speed";
operatingInfoCell.detailTextLabel.text = recentEvent.avgSpeed;
return operatingInfoCell;
break;
//MaxSpeed Row
case 2:
operatingInfoCell.textLabel.numberOfLines = 0;
operatingInfoCell.textLabel.textAlignment = UITextAlignmentCenter;
operatingInfoCell.textLabel.text = @"Max Speed";
operatingInfoCell.detailTextLabel.text = recentEvent.maxSpeed;
return operatingInfoCell;
break;
//Lifetime Row
case 3:
operatingInfoCell.textLabel.numberOfLines = 0;
operatingInfoCell.textLabel.textAlignment = UITextAlignmentCenter;
operatingInfoCell.textLabel.text = @"Lifetime";
operatingInfoCell.detailTextLabel.text = recentEvent.lifetime;
return operatingInfoCell;
break;
}
break;
//Alert Section
//==========================right here=========================================
Alert *alert = [[allAlerts objectAtIndex:indexPath.row];
//[alert retain];
case 2:
alertCell.textLabel.text = alert.date;//@"Date";
alertCell.detailTextLabel.text = @"Alert Message";
return alertCell;
//End of Outside Switch Statement
default:
break;
}
return attributeCell; //For the Compiler
}
Upvotes: 2
Views: 3470
Reputation: 21460
You can declare a variable inside the case of a switch statement by using braces like this:
case 2: {
Alert *alert = [allAlerts objectAtIndex:indexPath.row];
alertCell.textLabel.text = alert.date;//@"Date";
alertCell.detailTextLabel.text = @"Alert Message";
return alertCell;
} break;
Upvotes: 7
Reputation: 70733
You could try declaring:
Alert *alert = nil;
before the switch statement (perhaps at the beginning of the method), and just use the assignment:
alert = [allAlerts objectAtIndex:indexPath.row];
inside the switch statement when the row is valid.
Upvotes: 0