如何在UITableView的switch語句中創建一個變量?
我在建立一個tableView,它有三個部分。 我有前兩個工作,但最後一個是有點抵抗。 我的問題似乎涉及試圖在switch語句內聲明一個變量,實際上是一個嵌套的switch語句。 從我讀過的這不是一個好主意,但在這種情況下,它似乎是唯一的選擇。
有問題的部分動態地容納與特定設備相關聯的Alert對象的數量。警報來自核心數據並取代「日期」和「警報消息」我想顯示警報中的信息。我正在使用NSFetchRequest檢索相關的警報。這將返回按照我想要的方式排序的Alert對象數組。要在使用
Alert *alert = [allAlerts objectAtIndex:indexPath.row];
看來我不能宣佈在switch語句中的變量該行正確的警報顯示在的cellForRowAtIndexPath我一直在試圖拉回到正確的信息。任何想法如何解決這個問題?我將不得不在doSelectRowForIndexPath中做類似的事情,因爲我需要在選中單元格時推送一個詳細視圖。
我試過在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
}
我最初分配警報到零,因爲將消息發送到零是一個NOP,而一個錯誤。 – hotpaw2 2011-02-04 20:33:24
我試過這個,但沒有奏效。然而添加花括號卻有訣竅。謝謝。 – Aaronium112 2011-02-06 13:47:30