如何在UITableView的switch语句中创build一个variables?

在这里输入图像说明

我正在build立一个有三个部分的tableView。 我有前两个工作,但最后一个是有点抵抗。 我的问题似乎涉及到试图在switch语句中声明一个variables,实际上是一个嵌套的switch语句。 从我读过的这不是一个好主意,但在这种情况下,它似乎是唯一的select。

所讨论的部分dynamic地容纳与特定设备相关联的Alert对象的数量。 警报来自核心数据,并代替“date”和“警报消息”我想显示警报的信息。 我正在使用NSFetchRequest检索相关的警报。 这将返回按照我想要的方式sorting的Alert对象数组。 要在cellForRowAtIndexPath中显示正确的信息我一直在试图拉回正确的警报

Alert *alert = [allAlerts objectAtIndex:indexPath.row]; 

看来我不允许在switch语句中声明一个variables。 任何想法如何能解决这个问题? 我将不得不在didSelectRowForIndexPath中做类似的事情,因为我需要在选中单元格时推送一个详细视图。

我已经尝试在switch语句之外声明variables,但是这不起作用,因为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 } 

你可以通过使用大括号来在switch语句的情况下声明一个variables:

 case 2: { Alert *alert = [allAlerts objectAtIndex:indexPath.row]; alertCell.textLabel.text = alert.date;//@"Date"; alertCell.detailTextLabel.text = @"Alert Message"; return alertCell; } break; 

你可以尝试声明:

 Alert *alert = nil; 

在switch语句之前(也许在方法的开始处),然后使用赋值:

 alert = [allAlerts objectAtIndex:indexPath.row]; 

当行有效时,在switch语句内部。

本文概述了确切的问题以及如何解决它: Objective-C中的switch语句 。