【问题标题】:How do I create a variable inside a switch statement for a UITableView?如何在 UITableView 的 switch 语句中创建变量?
【发布时间】:2011-02-04 20:26:02
【问题描述】:

我正在构建一个包含三个部分的 tableView。 我有前两个工作,但最后一个有点抗拒。 我的问题似乎涉及尝试在 switch 语句中声明一个变量,实际上是一个嵌套的 switch 语句。 从我读到的内容来看,这不是一个好主意,但在这种情况下,它似乎是唯一的选择。

有问题的部分动态地容纳与特定设备关联的警报对象的数量。警报来自核心数据,而不是“日期”和“警报消息”,我想显示来自警报的信息。我正在使用 NSFetchRequest 检索相关警报。这将返回一个按照我想要的方式排序的 Alert 对象数组。为了在 cellForRowAtIndexPath 中显示正确的信息,我一直在尝试使用

拉回该行的正确警报
Alert *alert = [allAlerts objectAtIndex:indexPath.row];

似乎我不允许在 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
}

【问题讨论】:

    标签: ios uitableview core-data


    【解决方案1】:

    您可以在 switch 语句的情况下使用大括号声明一个变量,如下所示:

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

    【讨论】:

      【解决方案2】:

      你可以尝试声明:

      Alert *alert = nil;
      

      在 switch 语句之前(可能在方法的开头),并且只使用赋值:

      alert = [allAlerts objectAtIndex:indexPath.row];
      

      当行有效时在switch语句内。

      【讨论】:

      • 我最初将警报分配给 nil,因为向 nil 发送消息是 NOP 而不是错误。
      • 我试过这个但没有用。不过,添加花括号确实起到了作用。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多