gpt4 book ai didi

ios - 如何在 UITableView 的 switch 语句中创建变量?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:10:34 26 4
gpt4 key购买 nike

enter image description here

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

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

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

似乎不允许在 switch 语句中声明变量。有什么想法可以解决这个问题吗?我将不得不在 didSelectRowForIndexPath 中做类似的事情,因为我需要在选择单元格时推送详细 View 。

我试过在 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
}

最佳答案

你可以像这样使用大括号在 switch 语句的 case 中声明一个变量:

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

alertCell.textLabel.text = alert.date;//@"Date";
alertCell.detailTextLabel.text = @"Alert Message";
return alertCell;
} break;

关于ios - 如何在 UITableView 的 switch 语句中创建变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4902568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com