UITableViewのswitch文の中に変数を作成するにはどうすればよいですか?
私は3つのセクションを持つtableViewを構築しています。 私は最初の2つが働いているが、最後の1つは少し抵抗しています。 私の問題は、実際には入れ子になったswitch文のswitch文の中で変数を宣言しようとすることを含んでいるようです。 これは良いアイデアではありませんが、この場合は唯一の選択肢のようです。
このセクションでは、特定の機器に関連付けられているアラートオブジェクトの数に動的に対応しています。アラートはコアデータから来ており、「日付」と「アラートメッセージ」の代わりに、アラートから情報を表示したいとします。 NSFetchRequestを使用して関連するアラートを取得しています。それは、私がそれらを必要とする方法でソートされたアラートオブジェクトの配列を返します。私は、行の正しい警告が
Alert *alert = [allAlerts objectAtIndex:indexPath.row];
を使用していることを引き戻すしようとしてきたcellForRowAtIndexPathで適切な情報を表示するには私はswitchステートメント内の変数を宣言することはできませんよと思われます。どのように私はこれを回避することができます任意のアイデア?セルが選択されたときに詳細ビューをプッシュする必要があるため、didSelectRowForIndexPathで同様のことを行う必要があります。
私はswitch文の外で変数を宣言しようとしましたが、それは動作しません。なぜなら、index.rowはAlertの配列に存在しないインデックスにオブジェクトを要求し、プログラムをクラッシュさせるからです。
問題のコードは、この方法の一番下にある:
- (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
}
パーフェクト、それは今動作します。ありがとうございました。 – Aaronium112