【问题标题】:What alternatives to a switch statement could I use to update my UITableViewCells? [duplicate]我可以使用什么替代 switch 语句来更新我的 UITableViewCells? [复制]
【发布时间】:2011-11-12 05:09:32
【问题描述】:

可能重复:
Alternative to switch statement in objective C

我有来自 url 的 json 数据,我使用 switch 语句在表格单元格中显示这些数据。由于我只有 6 个单元格,因此我使用了 switch 语句,但我很想知道是否有任何其他方法可以代替 switch 语句。

switch(indexPath.row)
    {
        case 0 :
            cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",[dictionary valueForKey:@"firstname"],
                                   [dictionary valueForKey:@"lastname"]];
            break;

        case 1:
            cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Address",[dictionary valueForKey:@"address"]];
            break;

        case 2:
            cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Age",[dictionary valueForKey:@"age"]];
            break;

        case 3:
            cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Occupation",[dictionary valueForKey:@"occupation"]];
            break;

        case 4:
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Rating",[rate valueForKey:@"average"]];
            break;

        case 5: 
            cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Total Rating",[rate valueForKey:@"totalRatings"]];
            break;
            }

【问题讨论】:

  • 我已经添加了代码。你能指导我吗
  • 因为您的每个 switch 语句案例使用不同的逻辑(尤其是案例 4),所以没有绝对简单的解决方案。
  • stackoverflow.com/questions/8090251/… 。我试图回答一个类似的问题。也许它会帮助你。
  • 一个糟糕的答案是...... else if ...
  • 这正是switch语句的用例!

标签: iphone objective-c ios xcode uitableview


【解决方案1】:

这是一种完全过度设计问题的方法,但您可能会在其中找到一些有用的方法来帮助您解决特定问题:

typedef enum MONTableViewCellID {
    MONTableViewCellID_Name = 0,
    MONTableViewCellID_Address,
    MONTableViewCellID_Age,
    MONTableViewCellID_Occupation,
    MONTableViewCellID_Rating,
    MONTableViewCellID_TotalRating
} MONTableViewCellID;

@interface MONTableViewStuff : NSObject
{
@private
    NSDictionary * dictionary;
    NSDictionary * rate;
    UITableViewCell * cell;
    NSIndexPath * indexPath;
}
@end

@implementation MONTableViewStuff

- (UITableViewCellAccessoryType)tableViewAccessoryTypeForRow:(NSUInteger)row
{
    if (MONTableViewCellID_Rating == row) {
        return UITableViewCellAccessoryDisclosureIndicator;
    }
    return UITableViewCellAccessoryNone;
}

- (NSString *)lhsTextForRow:(NSUInteger)row
{
    switch (row) {
        case MONTableViewCellID_Name :
            return [dictionary objectForKey:@"firstname"];
        case MONTableViewCellID_Address :
            return @"Address";
        case MONTableViewCellID_Age :
            return @"Age";
        case MONTableViewCellID_Occupation :
            return @"Occupation";
        case MONTableViewCellID_Rating :
            return @"Rating";
        case MONTableViewCellID_TotalRating :
            return @"Total Rating";
        default : {
            assert(0 && "invalid row");
            return @"";
        }
    }
}

- (NSString *)rhsTextForRow:(NSUInteger)row
{
    switch (row) {
        case MONTableViewCellID_Name :
            return [dictionary objectForKey:@"lastname"];
        case MONTableViewCellID_Address :
            return [dictionary objectForKey:@"address"];
        case MONTableViewCellID_Age :
            return [dictionary objectForKey:@"age"];
        case MONTableViewCellID_Occupation :
            return [dictionary objectForKey:@"occupation"];
        case MONTableViewCellID_Rating :
            return [rate objectForKey:@"average"];
        case MONTableViewCellID_TotalRating :
            return [rate objectForKey:@"totalRatings"];
        default : {
            assert(0 && "invalid row");
            return @"";
        }
    }
}

- (NSString *)separatorForRow:(NSUInteger)row
{
    switch (row) {
        case MONTableViewCellID_Name :
            return @" ";
        case MONTableViewCellID_Address :
        case MONTableViewCellID_Age :
        case MONTableViewCellID_Occupation :
        case MONTableViewCellID_Rating :
        case MONTableViewCellID_TotalRating :
            return @" : ";
        default : {
            assert(0 && "invalid row");
            return @"";
        }
    }
}


- (NSString *)textLabelTextForRow:(NSUInteger)row
{
    return [NSString stringWithFormat:@"%@%@%@", [self lhsTextForRow:row], [self separatorForRow:row], [self rhsTextForRow:row]];
}

- (void)updateTextLabel
{
    cell.textLabel.text = [self textLabelTextForRow:indexPath.row];
    cell.accessoryType = [self tableViewAccessoryTypeForRow:indexPath.row];
}

@end

【讨论】:

  • 爱它。需要更多单例;)
  • @jrturton =) 哦,是的!单例答案在邮件中;)
  • 出于好奇,您为什么在方法签名中使用NSUIntegerMONTableViewCellID 会不会多一点自我记录?
  • @Paul.s 1) 因为我很快就敲掉了插图(在两个答案中),而且没有太多考虑。同样,它们都不包含错误检查。 2)我同意,它会的。 +1
【解决方案2】:

另一种过度设计的选择是基于对象的方法:

MONTableViewStuff.h

typedef enum MONTableViewCellID {
    MONTableViewCellID_Name = 0,
    MONTableViewCellID_Address,
    MONTableViewCellID_Age,
    MONTableViewCellID_Occupation,
    MONTableViewCellID_Rating,
    MONTableViewCellID_TotalRating
} MONTableViewCellID;

@interface MONTableViewStuff : NSObject

@property (nonatomic, copy, readonly) NSDictionary * dictionary;
@property (nonatomic, copy, readonly) NSDictionary * rate;

@end

MONTableViewStuff.m

@implementation MONTableViewStuff

@synthesize dictionary;
@synthesize rate;

- (id)init
{
    self = [super init];
    if (0 != self) {
        /* create an array of presenters ordered by MONTableViewCellID */
        presenters =
          [NSArray arrayWithObjects:
             [[NamePresenter new] autorelease],
             [[AddressPresenter new] autorelease],
             [[AgePresenter new] autorelease],
             [[OccupationPresenter new] autorelease],
             [[RatingPresenter new] autorelease],
             [[TotalRatingPresenter new] autorelease],
             nil
         ];
    }
    return self;
}

- (void)updateTableViewCell
{
    NSObject<MONUITableViewCellPresenter>* presenter = [presenters objectAtIndex:indexPath.row];
    [presenter updateUITableViewCell:cell tableViewStuff:self];
}

@end

演示者的界面如下所示:

@protocol MONUITableViewCellPresenter < NSObject >
@required
- (void)updateUITableViewCell:(UITableViewCell *)cell tableViewStuff:(MONTableViewStuff *)tableViewStuff;
@end

// our base presenter which handles the cells
@interface DefaultPresenter : NSObject

/** @return UITableViewCellAccessoryNone */
- (UITableViewCellAccessoryType)cellAccessoryTypeForTableViewStuff:(MONTableViewStuff *)tableViewStuff;

- (void)updateUITableViewCell:(UITableViewCell *)cell tableViewStuff:(MONTableViewStuff *)tableViewStuff;

@end

// required overrides
@protocol DefaultPresenterSubclass <MONUITableViewCellPresenter>
@required
- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff;
@end

// our specializations
@interface NamePresenter : DefaultPresenter <DefaultPresenterSubclass>
@end

@interface AddressPresenter : DefaultPresenter <DefaultPresenterSubclass>
@end

@interface AgePresenter : DefaultPresenter <DefaultPresenterSubclass>
@end

@interface OccupationPresenter : DefaultPresenter <DefaultPresenterSubclass>
@end

@interface RatingPresenter : DefaultPresenter <DefaultPresenterSubclass>
@end

@interface TotalRatingPresenter : DefaultPresenter <DefaultPresenterSubclass>
@end

他们的实现看起来像这样:

@implementation DefaultPresenter

- (UITableViewCellAccessoryType)cellAccessoryTypeForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
#pragma unused (tableViewStuff)
    return UITableViewCellAccessoryNone;
}

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
#pragma unused (tableViewStuff)
    assert(0 && "specialization required");
    return 0;
}

- (void)updateUITableViewCell:(UITableViewCell *)cell tableViewStuff:(MONTableViewStuff *)tableViewStuff
{
    cell.accessoryType = [self cellAccessoryTypeForTableViewStuff:tableViewStuff];
    cell.textLabel.text = [self cellTextForTableViewStuff:tableViewStuff];
}

@end

@implementation NamePresenter

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
    return [NSString stringWithFormat:@"%@ %@",[tableViewStuff.dictionary valueForKey:@"firstname"], [tableViewStuff.dictionary valueForKey:@"lastname"]];
}

@end

@implementation AddressPresenter

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
    return [NSString stringWithFormat:@"%@ : %@",@"Address",[tableViewStuff.dictionary valueForKey:@"address"]];
}

@end

@implementation AgePresenter

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
    return [NSString stringWithFormat:@"%@ : %@",@"Age",[tableViewStuff.dictionary valueForKey:@"age"]];;
}

@end

@implementation OccupationPresenter

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
    return [NSString stringWithFormat:@"%@ : %@",@"Occupation",[tableViewStuff.dictionary valueForKey:@"occupation"]];
}

@end

@implementation RatingPresenter

+ (UITableViewCellAccessoryType)cellAccessoryType
{
    return UITableViewCellAccessoryDisclosureIndicator;
}

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
    return [NSString stringWithFormat:@"%@ : %@",@"Rating",[tableViewStuff.rate valueForKey:@"average"]];
}

@end

@implementation TotalRatingPresenter

- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
    return [NSString stringWithFormat:@"%@ : %@",@"Total Rating",[tableViewStuff.rate valueForKey:@"totalRatings"]];
}

@end

【讨论】:

    【解决方案3】:

    您可以做的就是制作某种数据对象,其中包含您需要在您的 stringWithFormat 方法中显示的信息。

    然后创建一个 NSDictionary,将这些对象与索引进行键值对...

    但在这种特殊情况下,所有这些工作都值得吗?
    我对此表示怀疑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      相关资源
      最近更新 更多