如何在UITableView的switch语句中创建一个变量? [英] How do I create a variable inside a switch statement for a UITableView?

查看:160
本文介绍了如何在UITableView的switch语句中创建一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有问题的部分动态适应的数量与特定设备相关联的警报对象。警报来自核心数据,并且代替日期和警报消息我想显示警报中的信息。我使用NSFetchRequest检索相关的警报。这返回一个Alert对象数组,按我想要的方式排序。要在cellForRowAtIndexPath中显示正确的信息,我一直在尝试使用

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];

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

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.

我已经尝试声明变量switch语句,但是这不会工作,因为index.row可以要求一个索引的对象,不存在于Alert数组并且崩溃了程序。

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
}


推荐答案

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

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;

这篇关于如何在UITableView的switch语句中创建一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆